batch file help

October 27, 2011 at 14:21:25
Specs: Windows 7
hey everyone so been trying to figure what im doing wrong for a while maybe someone can help. i have files in a directory as given below


->>06/24/2009 06:02 PM 3,350,944 2009_06_24 [17 02] - IMG_002.JPG
06/24/2009 06:01 PM 3,288,493 2009_06_24.JPG
06/25/2009 01:39 PM 3,339,684 2009_06_25 [12 39] - IMG_003.JPG
06/26/2009 09:01 AM 3,230,748 2009_06_26 [08 01] - IMG_004.JPG
06/26/2009 09:14 AM 3,308,769 2009_06_26 [08 14] - IMG_005.JPG
06/26/2009 01:00 PM 3,502,048 2009_06_26 [12 00] - IMG_006.JPG
06/26/2009 01:01 PM 3,613,092 2009_06_26 [12 01] - IMG_007.JPG
06/27/2009 05:10 AM 3,185,607 2009_06_27 [04 10] - IMG_008.JPG
06/27/2009 05:20 AM 3,269,100 2009_06_27 [04 20] - IMG_009.JPG
06/28/2009 03:48 AM 3,015,406 2010_06_28 [02 48] - IMG_010.JPG
06/28/2009 03:56 AM 3,547,101 2010_06_28 [02 56] - IMG_011.JPG
06/30/2009 08:44 AM 625,468 2010_06_30 [07 44] - IMG_012.JPG
06/30/2009 08:45 AM 1,585,232 2010_06_30 [07 45] - IMG_013.JPG
07/18/2009 12:03 PM 1,404,235 2010_07_18 [11 03] - IMG_014.JPG

i am trying to extract the file name and create a foldername with only the date of file.
example.
file name = 2009_06_24 [17 02] - IMG_002.JPG
folder name = 2009_06_24
then i moves file name into that folder.

this is what i have so far.

@echo on

for /f %%a IN ('dir "*.jpg" /b') do (
set folder = %%a

mkdir "%folder%" (for some reason when i put mkdir "%%a" it creates the folder but then i cant move anything into the folder. because it will not store %folder%)

for /f "tokens=*" %%a IN ('dir "*.jpg" /b') do (
set filename=%%a

copy "%filename%" "%folder%"
pause

)
)


OUTPUT: (1 pass)

set folder = 2009_06_24
mkdir ""
for /F "tokens=*" %a IN ('dir "*.jpg" /b') do (
set filename=2009_06_24
copy "" ""
pause
)
)
The filename, directory name, or volume label syntax is incorrect.

set filename=2009_06_24 [17 02] - IMG_002.JPG
copy "" ""
pause
)
The system cannot find the path specified.
Press any key to continue . . .

thank you guys in advance if you can help me out. i'm pretty much a beginner with batch


See More: batch file help

Report •

#1
October 27, 2011 at 15:08:18
okay so i worked on it some more. i got it to create all the folder with the date names. and i was able to move the files into those folders, however it was moving ALL the jpg into each folder which is understandable because of the inside forloop. so i am trying to modify the filename string so it compares it to the _prefix (which should be just the first 10 characters of the filename) and if they match i want it to copy file into that folder. if not, then move on to the next date.

problem i am having is with string manipulation. i believe it is not shortening the filename and the _prefix variable is wrong.
Please help me with this issue. Thank you!

BELOW IS CODE I HAVE SO FAR


@echo on

for /f "tokens=1" %%a IN ('dir "*.jpg" /b') do (
set folder = %%a

mkdir "%%~na"


for /f "tokens=*" %%G IN ('dir "*.jpg" /b') do (
SET filename = %%G
SET _prefix=%filename:~0,10% <--- HELP!!!

if _prefix == %%~na ( <--- HELP!!!
copy "%%G" "%%~na"
) else (
echo(NO MORE MATCHES)
)
\
pause

)
)


Report •

#2
October 27, 2011 at 19:40:46
Apart from any other considerations, to expand environment variables inside a For loop you must enable delayed expansion. At the command prompt enter Set /? and Setlocal /? for descriptions and optional arguments.


Please come back & tell us if your problem is resolved.


Report •
Related Solutions


Ask Question