0

I'm trying to write a batch file that iterates through each filename, takes part of the strings, and sets the created and modified dates. Mind you, dirpath is a placeholder for actual path.

@echo off
setlocal enabledelayedexpansion
FOR /R dirpath %F in (*.*) DO (SET FNAME=%~nF SET MM=%FNAME:~0,2% SET DD=%FNAME:~2,2% SET YY=%FNAME:~4,2% nircmd.exe setfiletime %F "%DD% %MM%-20%YY% 00:00:00" "%DD%-%MM%-20%YY% 00:00:00")

I'm not sure how to even troubleshoot what the script is doing - pause doesn't pause the program after the loop. How can this be rewritten to function?

5
  • 1
    Please give us a clue. What doesn't work? Any error messages?
    – DavidPostill
    Oct 15, 2015 at 19:24
  • 1
    Start by commenting out the @echo off. Read Debugging your batch files
    – DavidPostill
    Oct 15, 2015 at 19:24
  • I really don't know how to answer that question, I don't know how to make the script pause rather than finish in a brief milisecond. Oct 15, 2015 at 19:25
  • This is what the file gives (. and dirname replaces what actually is): .>REM @echo off .>setlocal enabledelayedexpansion F was unexpected at this time. .>FOR /R dirname F in (.) DO Oct 15, 2015 at 19:46
  • Please don't change the question. You invalidate all the comments in my answer. Question rolled back.
    – DavidPostill
    Oct 15, 2015 at 20:44

2 Answers 2

2

How can this be rewritten to function?

You have a number of problems with your batch file:

  • It's not %f. You need use %%f instead. %f is for a command line, that needs fixing in at least 3 places.
  • I would put the commands inside the do ( ... ) on separate lines.
  • You can put a pause before the closing )
  • You can comment out the first line with rem to see what the batch file is doing.
  • Add echo to the payload line with nircmd.
  • Remove the echo when you think it's working.
  • You need in some places to use ! instead of % inside the forloop (that's what enabledelayedexpansion is for - it enables variables to be expanded at execution time rather than at parse time

Start with the following batch file (I've fixed the main mistakes for you):

@echo off
setlocal enabledelayedexpansion
for /r dirpath %%F in (*.*) DO (
  set FNAME=%%~nF
  SET MM=!FNAME:~0,2!
  SET DD=!FNAME:~2,2!
  SET YY=!FNAME:~4,2!
  echo nircmd.exe setfiletime %%F "!DD!-!MM!-20!YY! 00:00:00" "!DD!-!MM!-20!YY! 00:00:00"
  pause
  )
endlocal

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
  • for /r - Loop through files (Recurse subfolders).
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • setlocal - Set options to control the visibility of environment variables in a batch file.
2
  • It doesn't quite work - I think it is close. FNAME is still not being set to the filename. Oct 15, 2015 at 19:58
  • @alrightgame Answer updated. It should work now.
    – DavidPostill
    Oct 15, 2015 at 20:44
1

I found another solution by simply calling a function.

FOR /R dirname %%F in (*.*) DO call :Setfiletime %%F
goto End
:Setfiletime
   SET FNAME=%~n1
   echo %FNAME%
   SET MM=%FNAME:~0,2%
   SET DD=%FNAME:~2,2%
   SET YY=%FNAME:~4,2%
   nircmd.exe setfiletime %1 "%DD%-%MM%-20%YY%" "%DD%-%MM%-20%YY%"
:goto :eof
:End

Thanks everyone for all your help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .