1.

Solve : Create folder from first 5 chars of filename (and move the file to it.)?

Answer»

Here's what I have.

Code: [Select]C:\test\12345-test1.txt
C:\test\23456-test2.txt
C:\test\44444-test3.docI would like to have the script read the first 5 digits from the file, create a folder with those digits, and include the letter T before it. (and then move the file into that folder)

So, the result after running:

Code: [Select]C:\test\T12345\12345-test1.txt
C:\test\T23456\23456-test2.txt
C:\test\T44444\44444-test3.docCurrently, I have this, which does the funciton, I just can't figure out where to put the SET command to extract the 5 characters.
Code: [Select]echo off
for %%a in (*.*) do (
md "T%%a" 2>nul
move "%%a" "T%%~a" 
)I think this needs to be added to choose only the first 5 chars...
Code: [Select]set first5=%%a:~5,1%Thoughts?
echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set FILENAME=%%A
set first5=!filename:~1,5!
md "T!first5!" 2>nul
move "%%a" "T!first5!"
)This WORKED,

echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set filename=%%a
set first5=!filename:~1,5!
md "T!first5!" 2>nul
move "%%a" "T!first5!"
)

EXCEPT the capital A in line 4 had to be lowercase. Quote from: billiehawkins on October 25, 2011, 01:09:33 PM

Except the capital A in line 4 had to be lowercase.

Yes, I made a typo. I am so used to using capital letters for the FOR variables. There is a reason - SEE if you can spot it in the FOR help. I looked hard but I couldn't see the word "thanks" anywhere in your post...

This has been tested and works with the corrected A to a case-change.

Thank you for the support on this, I'm learning so much about For loops from these tasks!
Brilliant stuff guys  Thanks a lot.

I adaped your script to pickout the date from the file name of a PDF report, then move that report to a folder with that date ( for archiving): Example file name:  R9621_XJDE001_D1111201_T161418976.pdf

ECHO OFF
setlocal enabledelayedexpansion

for /f %%a IN ('dir /b T:\B7333\PrintQueue\*.pdf') do (
set filename=%%a
set last21=!filename:~-21!
Set pdfdate=!last21:~0,6!
mv %%a pdfdate/

)

Quote from: billiehawkins on October 25, 2011, 01:09:33 PM
This worked,

echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set filename=%%a
set first5=!filename:~1,5!
md "T!first5!" 2>nul
move "%%a" "T!first5!"
)

Except the capital A in line 4 had to be lowercase.
Shouldn't that set statement be:
Code: [Select]set first5=!filename:~0,5!


Discussion

No Comment Found