-2

Edit1:

So it seems my loop runs to the very last record and then executes the mkdir and MOVE commands. I feel like I'm missing parentheses somewhere?

FOR %%i IN (%folderPath%\*.Pdf) DO SET fileName=%%i
  ECHO %fileName%
FOR /f "tokens=3-4 delims=_" %%a IN ("%fileName%") DO SET fileClientID=%%a_%%b
  ECHO %fileClientID%

REM Check to see if folders exist, and if they do not, create them
IF NOT EXIST "%folderPath%\%fileYear%" mkdir %folderPath%\%fileYear%

IF NOT EXIST "%folderPath%\%fileYear%\%fileMonth%" mkdir %folderPath%\%fileYear%\%fileMonth%

IF NOT EXIST "%folderPath%\%fileYear%\%fileMonth%\%fileClientID%" mkdir %folderPath%\%fileYear%\%fileMonth%\%fileClientID%

REM Moves the file from source path to destination path
SET fileSourcePath="%fileName%"
SET fileDestPath="%folderPath%\%fileYear%\%fileMonth%\%fileClientID%"

MOVE "%fileSourcePath%" "%fileDestPath%"

PAUSE
Stephan
  • 53,940
  • 10
  • 58
  • 91
N.Ha
  • 71
  • 3
  • 9
  • Don't move several times. Evaluate the neccessary data create the structure and move only once. Use a `for` to iterate the files in the folder and parse the name with a `for /f` the `~z` modifier of the used for meta variable `%%x` will return the file date. There are literally hundreds of similar examples here on [SO]. –  Oct 25 '18 at 00:00
  • Please edit the question and paste in the text of the code you are trying to make work. No worry about how it looks now. We all started somewhere. You learn to write batch files by writing batch files. – lit Oct 25 '18 at 01:02
  • @lit sure thing – N.Ha Oct 25 '18 at 02:00
  • The filename variable needs to contain a filename that actually exists. Perhaps `SET "fileName=GRGID_ABM000001_ClientID_113_GlxyABM.pdf"` – lit Oct 25 '18 at 12:50
  • Hmm well I set that as static for now (though in the future I would need it to loop through the files of a directory) and it still returns the Echo is Off error when I try to call the variable. – N.Ha Oct 25 '18 at 14:32
  • So many syntax errors i.e `mkdir` at end of line with no arguments. At the end of code, `%fileName%` is double quoted, then sets the value to `%fileSourcePath%` which is double quoted again. Try correcting the syntax as your code lines look like it has been wrapped to a certain line length without using a continuation character `^`. – michael_heath Oct 25 '18 at 22:20
  • yes, you are missing parantheses. A `(` in the first line between `DO` and `SET` and the corresponding `)` before the `pause` (or after the `pause` if you want to pause after each move) NOTE: You will need [delayed expansion](https://stackoverflow.com/a/30284028/2152082) for some of your variables. – Stephan Oct 26 '18 at 08:33
  • Not bad for a first script `:)`. But please remove your working code from the question and put it in an answer instead. Questions should not contain the solution. – Stephan Oct 27 '18 at 09:04

1 Answers1

0

So I finally finished the code and it all works as it should. I really appreciate everyone's help getting me to this point as this is my first batch file script I've ever written. Final product below and of course if there are any best practices or more efficient ways of writing it, I'd love the feedback. Thanks again!

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

SET folderPath="C:\Users\nha\Desktop\FolderMover\Test"

FOR %%a IN (%folderPath%\*.Pdf) DO (
  SET fileDate=%%~ta
  SET fileName=%%a
FOR /f "tokens=1,3 delims=/, " %%a IN ("!fileDate!") DO (
  SET fileYear=%%b
  SET fileMonth=%%a
For /f "tokens=3-4 delims=_" %%a IN ("!fileName!") DO (
  SET fileClientID=%%a_%%b
IF NOT EXIST !folderPath!\!fileYear!\!fileMonth!\!fileClientID! mkdir !folderPath!\!fileYear!\!fileMonth!\!fileClientID!
  SET fileSourcePath=!fileName!
  SET fileDestPath=!folderPath!\!fileYear!\!fileMonth!\!fileClientID!
  MOVE !fileSourcePath! !fileDestPath!
    )
  )
) 
Stephan
  • 53,940
  • 10
  • 58
  • 91
N.Ha
  • 71
  • 3
  • 9
  • 1
    for as "best practices": Always enclose paths with quotes (to avoid syntax errors in case they contain spaces or other poisonous characters). Use preferred syntax for `set`: `set "var=value"` (note the position of the quotes) to avoid unintended spaces and avoid syntax errors with poisonous characters. Use just one `setlocal enableextensions enabledelayedexpansion` – Stephan Oct 28 '18 at 11:02
  • @Stephan Side question. If i wanted to change the folderPath to a folder that is located on what seems like a network drive that looks like this \\gfrgts.gfrg.local\PrintDocuments do I need some kind of special key words to make it work? – N.Ha Oct 31 '18 at 17:25