Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Create Folder by filename

28 views
Skip to first unread message

mitbuerger

unread,
May 10, 2012, 4:09:11 AM5/10/12
to
Hi

I have a folder full of different test files. The filename ist structured like this:

G#ID#TID.txt

so it's G for Gender (M/F), #ID for Tester-ID and #EID for exercice answer id. f.e. its F052M01S.txt.

So my plan is to put all test files for F001 into the folder "F001". How can i solve this? Must be something like

> for /F "tokens=*" %%f in ('dir /S /b %CD%\*.TXT') do (
> mkdir %%~nf
> copy "%%f" "%%~nf\%%f"
> )

but then F052M01S.txt would be in F052M01S\F052M01S.txt and not F052\F052M01S.txt.

simply... i don't know how to excerpt the first x letters of a varname ;-)
thx für help.

bye

foxidrive

unread,
May 14, 2012, 7:43:09 PM5/14/12
to
Your description isn't very clear. If you want to copy F001*.txt to a folder called F001 then this may work with your file structure (untested).

You can replace F001 with %1 and then use the batch file to do different numbers.

@echo off
set var=F001
for /F "tokens=*" %%f in ('dir /b *.TXT ^|find "%var%"') do (
mkdir "%var%" 2>nul
copy "%%f" "%var%"
)





--
Mic

Auric__

unread,
May 14, 2012, 7:45:24 PM5/14/12
to
Try this:

setlocal enabledelayedexpansion
for %%a in (*.txt) do (
set FNAME=%%a
call :Loop
)
:Loop
set FPART=%FNAME:~0,4%
if not exist %FPART%\nul mkdir %FPART%
copy "%FNAME%" %FPART%
goto :EOF

If it works the way you want it to, change "copy" to "move".

--
Fear strips away all pretense.

foxidrive

unread,
May 14, 2012, 7:52:33 PM5/14/12
to
On 15/05/2012 9:45 AM, Auric__ wrote:
>
> Try this:

What is delayed expansion enabled when you aren't using it? :)

It could all be done in a loop with delayedexpansion

> setlocal enabledelayedexpansion
> for %%a in (*.txt) do (
> set FNAME=%%a
> call :Loop
> )
> :Loop
> set FPART=%FNAME:~0,4%
> if not exist %FPART%\nul mkdir %FPART%

the \nul trick is deprecated now.

For a folder this is reliable in NT windows: if exist "d:\folder\"


> copy "%FNAME%" %FPART%
> goto :EOF
>
> If it works the way you want it to, change "copy" to "move".
>


--
Mic
0 new messages