0

I have a directory where users are saving their excel files that should adhere to a specific naming convention:

XX-TestFile.xlsx

where XX is a variable digit and -TestFile.xlsx should always be the same and not change. I'd like to be able to check through a batch job if files in the directory adhere to this naming convention.

If filename is misspelled, i.e. XX-TetsFiel.xlsx, XX is not a digit like 02 or even XX-testfile.xlsx (all lowercase), then files should move to an Error directory.

I am using below to move the files to achieve this. I am testing with 11-testFiel.xlsx but when I execute the .bat nothing happens - I get no errors and the file remains where it was before:

@echo off

for /f "delims=" %%a in ('dir /b *.xlsx | findstr /v "[0-9][0-9]-TestFile.xlsx"') do move "%%a" "C:\Temp\Archive\Error"

Many thanks in advance for your help!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Aligator3000
  • 335
  • 3
  • 17
  • 4
    The pipe symbol `|` has to be escaped with a caret `^|` inside a for /f command. If you'd executed the batch in an open cmd window you would see the error message. –  Mar 21 '19 at 16:23
  • @LotPings: Thanks, that worked! – Aligator3000 Mar 21 '19 at 16:31
  • 1
    You need to escape `|` with the batch file escape character, the caret (`^`), so it will become `^|`. This happens because `|` is executed in higher prio than the `for` loop. – double-beep Mar 21 '19 at 16:34
  • @double-beep.. but LotPings already said that... why not just upvote his comment instead of repeating it? – Gerhard Mar 21 '19 at 17:18
  • @GerhardBarnard sorry, this is not a place to discuss that, but as you can see, I have added some explanations. – double-beep Mar 21 '19 at 18:28
  • Related: [Batch script with for loop and pipe](https://stackoverflow.com/q/6026773) – aschipfl Mar 21 '19 at 19:40
  • 'dir /b *.xlsx | findstr /v "[0-9][0-9]-TestFile.xlsx"' allows for 109 possible combinations since XX is a variable with two digits like 02. However, I would now only like to check for specific file names of 20 possible digits like 02-TestFile.xlsx, 05-TestFile.xlsx, 10-TestFile.xlsx, etc. This list is fixed...Can you suggest a solution for this? – Aligator3000 Mar 27 '19 at 13:23

0 Answers0