Question:
batch script: move files to folder if filename contains?
ej25_impreza
2008-10-01 13:15:38 UTC
i need a batch script that will look at filenames in a folder. if the filename contains "abc," i want it to be moved to a different folder. i'd also like to know how to do the opposite- if the filename does not contain "abc," then move it to some folder. is this possible?
Four answers:
Cloud
2008-10-05 08:57:15 UTC
To watch the folder test, If the filename contains "abc" then the script move it to folder c1, else move to c2



Here is the sample:

C:\>autoClassify test abc c1 c2

move "test\abc.txt" to c1

move "test\abcdef.txt" to c1

move "test\xyzabcok.txt" to c1

move "test\def.txt" to c2

move "test\defghi.txt" to c2

move "test\xyzdefok.txt" to c2



The demo screenshot:

http://img72.imageshack.us/img72/1290/autoclassifysm5.png



You can download "autoClassify.cmd" from

http://w14.easy-share.com/f/1325952221.html
Shwaa
2008-10-01 13:51:53 UTC
The dir command and Find command can be used to gether to do this.



Dir /b gives a bare file listing



Use a pipe character (|) to pipe the dir commands output into the find command:



Dir /b|Find "string"



That command will give you a list of all the files containing the "string"



If you output that into a txt file, then you can use that text file for the move:



Dir /b|Find "string" >> C:\FilesINeed.txt



Then do the move:



for %%a in (c:\filesINeed.txt) do move %%a [Destination]



That's what's known as a for loop. It will go throught the file C:\FilesINeed.txt and assign each line one at a time to the variable %%a. Then it will DO the command "move" %%a to the destination.



In order to do the opposite, just add the /V switch to the find command. That will return all files not containing the string.
cld
2008-10-01 13:27:46 UTC
@echo off



if %1="abc" then

move %1 \folder1

if %1=not "abc" then

move %1 \folder2



exit
anonymous
2014-11-13 01:47:47 UTC
very confusing step. search on to google or bing. that could actually help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...