3

Most of the examples on using Windows Batch For Loop is about using "IN".

FOR %%A IN (1 2 3 4) DO ECHO %%A

I need to use NOT IN instead. When I type something as:

FOR %%A NOT IN (1 2 3 4) DO ECHO %%A

It says "NOT was not expected at that time".

Thanks

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
Bill
  • 2,026
  • 9
  • 55
  • 99
  • 1
    What's *not in* 1..4? 0 then 5..infinity? What's the relationship to powershell? – Alex K. Sep 03 '13 at 11:47
  • That's just an example. I meant how to use NOT IN. I am checking against a text files that lists file names. thanks – Bill Sep 03 '13 at 11:55
  • AFAIK NOT is not valid with IN, you could set a variable inside the IN and if its not set after, its NOT IN? – Alex K. Sep 03 '13 at 11:57
  • There is no "NOT IN" feature. There is "IF NOT", but that's all. – Jon Sep 03 '13 at 11:57
  • I am looping over a list of files, I want to move all files in a directory that are NOT IN the list of file names in the text file. Is that doable? – Bill Sep 03 '13 at 12:00

4 Answers4

0

according to your comments above, you want to have a "DIR without those files"? Then this should work:

> type donotuse.txt

alpha.txt
beta.exe
gamma.cmd

> type t.bat

@dir /b | findstr /b /e /v /i /l /g:donotuse.txt

> dir /b

albetade.txt
alpha.exe
alpha.txt
beta.exe
donotuse.txt
gamma.cmd
t.bat

> t

albetade.txt
alpha.exe
donotuse.txt
t.bat

>

edit: dbenham is right. Added /i /l to my code.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    Very close, but you need the `/L` option to force literal interpretation of names instead of regex. You also need the `/I` option to force case insensitive matching. On general principal, you should use `/I` when working with Windows file names. But it is actually critical in this case to avoid a nasty FINDSTR bug. See [Why doesn't this FINDSTR example with multiple literal search strings find a match?](http://stackoverflow.com/q/8921253/1012053) – dbenham Sep 03 '13 at 14:07
0
for %%F in ('dir /b /a-d "somepath\*"^|findstr /vilxg:"exclusions.txt"') do echo %%F

Since in comments you say you want to move files not in a list, you should try using ROBOCOPY. Check out the /MOV and /XF options. It should allow you to do exactly what yo want in a more direct manner. Type robocopy /? from the command line for help. There are a ton of options to navigate, so it might take a while to get the exact result you want. But the command is extremely powerful and worthwhile.

dbenham
  • 127,446
  • 28
  • 251
  • 390
0

Bill, this might do what you need, or give you a hint...

@echo off
for /f "delims=" %%a in (' type "file.txt" ') do (
if not exist "c:\target\%%a" move "%%a" "c:\target"
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

I had the same problem, but no solution. So... that's my personal, simple, solution:

@echo off
rem set the path to check
set sourcePath=C:\Program Files (x86)\MySoftware
set destPath=c:\temp\MySoftware-StrangerFiles
set tempPath=c:\temp
set currentFilesListTxtFileName=MySoftware-allfiles.txt
set correctFilesListTxtFileName=MySoftware-correctFilesList.txt
set moveFilesListTxtFileName=MySoftware-moveFilesList.txt
echo Move not allowed files in a different path
cd "%sourcePath%"
mkdir %destPath%

call :correctFilesList

dir /B > c:\temp\MySoftware-allfiles.txt
findstr /V /G:%tempPath%\%correctFilesListTxtFileName% %tempPath%\%currentFilesListTxtFileName% > %tempPath%\%moveFilesListTxtFileName%
for /f "delims=" %%a in (' type "%tempPath%\%moveFilesListTxtFileName%" ') do move %%a %destPath%
pause

:correctFilesList
rem files whitelist
rem correctfiles can be changed with "dir /B path>>c:\temp\correctFilesList.txt" from a correct source. Or you can generate a list of correct files via command as below.
echo correctfile1.dll>> %tempPath%\%correctFilesListTxtFileName%.txt
echo correctfile2.log.xml>> %tempPath%\%correctFilesListTxtFileName%.txt
echo correctfile2.log.xml>> %tempPath%\MySoftware-correctFilesList.txt
Acca Emme
  • 356
  • 2
  • 11