Batch File, search all files in folder, then sort - batch-file

I've done some small batch files before, but never any that required file names being read in a folder.
I need a batch file that can sort a folder for me.
I need it to look into a folder, search each text file, if it contains a "string", then move it into an accepted folder, if not, move it into a rejected folder.
Any ideas how to tackle this?
I have used something as follows for finding text before.
find /c "string" file
if %errorlevel% equ 1 goto
I don't know how to make it search each file in a folder, and those file names change on a daily basis.

in your problem statement you said all of these files are .txt files. So I would recommend using a for loop to loop through each of the .txt files in the directory, and then use findstr to search for the intended string. The code I posted below will work in such a manner. Essentially it changes the directory to the desktop (or wherever your files are located), then loops through each .txt files and searches for the string. The findstr output is redirected to nul (for faster processing) and if this is successful, the .txt file is moved to C:\Users\Desktop\SuccessFolder, otherwise this file is placed in C:\Users\Desktop\FailureFolder. (Keep in mind the search "string", original location, and 2 folder locations still need to be set) I believe this code should work for what you are trying to accomplish. Let me know if any changes need to be made to the code =]
#Echo off
cd /D C:\Users\Desktop
FOR %%a in (*.txt) do (
findstr /C:"string" "%%a" >nul && move "%%a" "C:\Users\Desktop\SuccessFolder" || move "%%a" "C:\Users\Desktop\FailureFolder")

Related

Batch move files from multiple folders to single destination

I am looking to write a script to help with my research. I need to be able to move files from multiple subdirectories to a single destination directory. I have a list of files to be moved, but have no experience writing anything like this. Did some digging around and botched together this
#echo OFF
CHCP 65001 >NUL
FOR /F "usebackq delims=" %%I In ("F:\Coyote Pics\Final sorted pictures\COYCAM1.txt") DO (
xcopy /s "F:\camelot\Datasets\default\Media\%%I" "F:\Coyote Pics\Final sorted pictures\%%I*"
)
PAUSE
which depends on making a txt file of the file paths to be copied. However, when I run the script, I get "file not found". Not sure if this is the best way to do this or if there is an easier way.
For reference, here is an example of a list of files that I need to copy into a single destination folder.
F:\camelot\Datasets\default\Media\bd\bdb71a66-4a70-4f34-adcb-2a4792d7a736.jpg
F:\camelot\Datasets\default\Media\22\22976f1a-b718-43ac-89b1-5db2815f1a38.jpg
F:\camelot\Datasets\default\Media\01\013a0efe-12e0-40fd-9057-bec92ead8865.jpg
F:\camelot\Datasets\default\Media\47\47543fc8-7ba8-4989-b5e1-1e57052b5fd4.jpg
F:\camelot\Datasets\default\Media\08\08d84e39-7458-4e8e-88bd-b443b8e419f2.jpg
F:\camelot\Datasets\default\Media\8e\8e814897-2f6f-45d4-9be3-77d41c4fc836.jpg

Setting up Source and Destdir in a batch

for /L %%f in (1,1,10) do copy File1.txt %%f.txt
this code does the job very well, but I'm trying to understand how to change it to make it read
subfolders, so that I don't have to keep moving the batch file to every folder
I saw this, but not really sure how to put it together
#echo off
SET "sourcedir=C:\Users\user\Desktop\Main\Original"
SET "destdir=C:\Users\user\Desktop\Main\Copied"
for /L %%f in ('dir /s 1,1,10) do copy *.txt %%f.txt
in the section - copy *.txt %%f - I put a * so that it can only look for .txt files, but this action
slows down the coping and then stops working.
I know my code is a mess, just trying to put something together
I have many Subfolders and each folder has 1 txt file in it with all random names
and I need to make multiple copies of each file.txt in each folder
I have so many subfolders that it would literally take me months of time to get all files copied
and by then I would have many more new files to work on
so getting this copier to read Subfolders is like top priority for me.
I would like help putting this together and then explaining how it links
because I'm interested in applying the Set and dir to other batch file I have
Please any details on this will be much appreciated
I was told to look into xcopy and robocopy, but I have no idea were to add a counter
#echo off
for /1 %f in (1,1,10) do xcopy "C:\Sources" "C:\Target" /c /d /i /y
exit
So I have this that reads from source and dumps in main folder where the batch is
Option 1)
for /L %%f in (1,1,10) do xcopy "C:\Source Address\*.txt" %%f.txt
Option 2)
for /L %%f in (1,1,10) do xcopy "C:\Source Address\" "C:\Destination Address\ %%f.txt"
The thing I don't like is that is asks me a question
and I have over 10,000 txt files, I can't sit here and press F for Filename
10,000 times, can we disable that
OK, so I got this working
all I need help with is were to add this /c /d /i /y
I am still trying to get it to read Subfolders with the batch sitting
in the main folder and me not having to move files back and forth
Option 3)
for /L %%f in (1,1,110) do copy "C:\Source Address\*.txt" %%f.txt`
This works well with the Source and the wild card #magoo told me to add
the Source before the .txt file
But with this code I would still have to open hundreds of folders and move
the file to the source run the copier and then move back all copied files
Still need help with Subfolders
for /L %%f in (1,1,10) do copy File1.txt %%f.txt
will vary %%f from 1 (the first number in the parenthesised list) to 10 (the last) in steps of 1 (the middle) and will therefore copy file1.txt to 10 separate files, 1.txt to 10.txt.
Since there are no paths specified, the copy will be performed from file1.txt in the current directory to 1.txt .. 10.txt in the current directory.
If you were to put your batch file in any directory that is mentioned in the path variable (just execute path at the prompt to show it) then no matter where the current directory is, you could just use that batch filename to execute the batch, and the files would be created by copying file1.txt in the now-current directory to 1.txt .. 10.txt in the now-current directory - if file1.txt exists in the now-current directory, and if not, it will generate error messages reporting that the source file is missing.
If you were to replace file1.txt in the batch with "x:\wherever\file1.txt" then the file would be copied specifically from the file "x:\wherever\file1.txt" regardless of whether file1.txt exists in the now-current directory. (And please get used to "quoting file or pathnames" as it will avoid a whole slough of problems when you tackle names containing spaces and some other special characters).
I have no idea how for /L %%f in ('dir /s 1,1,10) do is supposed to work since according to Hoyle, the first element in the parenthesised list should be a number. I'd suggest that we have a small transcription problem here.
Th slower and slower problem - yes, understandable. You are creating more and more .txt files, and copying all of them to a new destination...
Perhaps referring to This response and question might show how to solve your subdirectory-scan issue.

Batch - Rename multiple files to sequential numbers

I need to rename multiple files at once. Lets say I have a these files:
episode1.mkv
e1.mkv
s01e01.mkv
As you see, the file names have nothing in common.
How can I change the names of all the files to numbers (1.mkv - 2.mkv - 3.mkv ...) using batch.
I want the first file to be renamed to 1.mkv (no letters or spaces or anything else in the name) the second file to be renamed to 2.mkv, and so on.
I've looked around the internet a lot and I still didn't find anything that does exactly this.
TIA
Batch file version
#echo off
setlocal ENABLEDELAYEDEXPANSION
set/a fileNum = 1
for %%f in (*.mp4) do (
ren %%~nf%%~xf !fileNum!%%~xf
set/a fileNum += 1
)
GUI version
FreeCommander can do this for you.
Run FreeComander
Navigate to the folder.
Select all the files you want to rename.
Hit F2
The rename dialogue will appear. To rename all files names to be numeric do this:
Click Rename to make the change.
I suppose you could make a FOR /L loop with a limit of the amount of files in your directory, feed it a DIR /B |FINDSTR .mkv line by line renaming each file to an index variable you keep. Since you are going to name them into just numbers your DIR command will spit the same movie back as the first item over and over so maybe write the output of DIR /B|FINDSTR .mkv to a text file and work off of that? Or move the file you rename to another folder?
Once you make a rough project we could help you refine it? Or use FreeCommander and make a batch to do the rest you want to do after that.

Replace file.old with file.new in multiple subfolders

I am trying to replace over 400 audio files, in just about as many sub-folders, with a updated format required by the program. It's the same file in each location but its how they require it for their xml file. I found several similar post, replacing files in multiple sub-directories like this one Replacing a file into multiple folders/subdirectories this I assumed would meet my needs.
I want to find all AR33.mp3 files in SFX'x folders, and replace with the new required AR33.wem my issue is as follows:
FOR /R C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX %%I IN (AR33.mp3) DO echo COPY /Y C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem %%~fI
This does the reverse and copies the file I want to overwrite, ar33.mp3, all over the folder structure in every folder where there shouldn't be any. but it does say 1 file copied after each entry.
(AR33) creates extension-less copies. adding (*AR33.mp3) does nothing, same with (*AR33.mp3 *) does nothing, even with "1 item copied" pointed out in the console. At this point I'm trying what I can to get this to work randomly doing stuff. Changing COPY to replace give a invalid switch -/y. Am I using the wrong "script" to do what I want? Because even changing one of the AR33.mp3 over to a .wem didn't see any change after replacing the line .mp3 to .wem. I assume I need to fix what its searching for and what its copying.
Updated bat:
FOR /R "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\" %%I IN (AR33.mp3) DO echo COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "%%~fI"
Output sample:
C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted>echo COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\AAC0\ShipName__PASC045\isPlayer__False\AR33.mp3"
COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\AAC0\ShipName__PASC045\isPlayer__False\AR33.mp3"
You want to replace .mp3 with .wem as well. For now you are simply copying the .wem file and naming it with a .mp3 extension. So we could just rename the .mp3, then copy the .wem file to it.:
#echo off
for /R "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\" %%I in (*AR33.mp3) do (
echo ren "%%~fI" "%%~nI.wem"
echo copy /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "%%~dpnI.wem"
)
Please take note before continuing. The above will only echho the result, once you are happy that it does what you want, then only remove `echo from both strings in the code block.
It is also always a good idea to backup files before running a big copy process like this.

Command Promp to copy ONLY images from FOLDERS and SUBFOLDERS

I'm wondering if it is possible to set up a batch command to perform this action.
Once .bat file is executed, ALL images from folders and sub-folders would be copied to my location on the desktop.
Example:
Original folder is located:
\intranet\file_location\PP Complete Images (in this folder will be loads of other folders and in those folders there will be .jpg images)
Destination file would be based on the desktop.
So I need to extract .jpg images from all folders and sub-folders.
If image already exists in original folder, skip the image or overwrite as script will be executed every morning.
Or should I look for a software to do this for me?
Existing code:
cd c:
cd\
copy "\\intranet\PP Complete Images\Master Image Folder*.jpg" "C:\Users\username\Desktop\Master Image Folder"
copy "\\intranet\PP Complete Images*.jpg"
exit
How do you want it?
It isn't quite clear to me, how the result exactly should be -- should it be flattened or should it be hierarchical as well?
Look at this for example:
source
folder-1
folder-1-1
image1.jpg
folder-1-2
image2.jpg
cheese.jpg
image3.jpg
some_text.txt
folder-2
folder-2-1
image3.jpg
some_music.mp3
cheese.jpg
target
Should the result be basically a copy of the shown hierarchy (without any other file than the jpgs), or should it be a flattened result like this one:
source
... (see above)
target
image1.jpg
image2.jpg
cheese.jpg
image3.jpg
image3.jpg
How can you do it?
Flattened
You can use DOS' for command to walk directories1 and make a custom function2 to handle the files:
#ECHO OFF
for /r %%f in (*.jpg) do call:copyFile %%f
GOTO END
:copyFile
copy /V /-Y %~1 ..\target
GOTO:EOF
:END
Meaning: for every %%f in the listing of *.jpg from the current working dir, execute function copyFile. The /r switch makes the listing recursing (walk through all subdirectories).
In the function, the argument passed to it (now known as %~1) is passed to the copy function: Copy the file to the target directory which is ..\target in this case. /V lets copy verify the result, /-Y lets it ask for permission to overwrite files. See copy /?!
Very big problem: If you have one or more files in different subfolders of your source directory, which have the same name (like the two cheese.jpgs in my example), you will loose data!
So, I wouldn't recommend this approach, as you risk loosing data (digital cameras are not very creative in naming pictures!).
Hierarchical
Just use robocopy:
robocopy /S <sourcedir> <targetdir> *.jpg
/S creates and copys subfolders as well. You can also use /DCOPY:T to make the directories have the same timestamp than the original ones or /L to preview the actions of robocopy.
Small problem: The /E switch handles subfolders as well, even if they are empty. /S handles subfolders as well, but not, if they are empty. But it handles them, if they are not empty, but have no JPG inside -- so, subfolders without JPGs will result in empty folders in the target folder.
Robocopy has loads of parameters, so check out robocopy /?.
Hope this helps! :-)
1Found here: How to traverse folder tree/subtrees in a windows batch file?
2Found here: http://www.dostips.com/DtTutoFunctions.php
Your existing code:
the cd c: is incorrect. To switch the current drive to c: use
c:
The cd \ is redundant. Your remaining code specifies the directories, so the current directory is irrelevant.
Your first copy command has three problems. Master Image Folder*.jpg means all filenames beginning Master Image Folder and ending .jpg. You probably meant Master Image Folder\*.jpg meaning all files ending .jpg in ...\Master Image Folder\
C:\Users\username\Deskto... is probably an error. It is a literal path, so the actual directory would be C:\Users\username\Deskto... You would probably need C:\Users\%username%\Deskto... to substitute-in the current username.
And then the job would stop on a filename-match, so either you'd be pressing A to overwrite all or you'd be pressing y or n for each name-match.
Your final copy command has no specified destination directory.
You can edit-in your actual code by using the edit button under the original text window, cutting-and-pasting your actual code - censoring if necessary, selecting the resultant code block and pressing the {} button above the edit box which indents each line with the effect of formatting and hilighting the code.
The simplest solution is probably to use
xcopy /d /y /s "\\intranet\PP Complete Images\Master Image Folder\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
which will copy updated files (/d) with automatic overwrite (/y) and scanning subdirectories (/s) from-name/mask to-directory.
This would create an identical directory-hierarchy to the original subtree under the destop's Master Image Folder directory.
You could extend this to
for %%a in (
"\\intranet\PP Complete Images\Master Image Folder"
"\\intranet\wherever\somewhere"
) do xcopy /d /y /s "%~a\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
to perform the same action on multiple directory-subtrees; but you need to ensure that the destination directory is not within any subtree selected for inclusion in the list within the parentheses.
I'd advise against "flattening" the output because if you do that, the latest whatever.jpg from each of the subtrees will end up in your destination directory, without notification that there are many possibly different whatever.jpg versions.
I do believe the solution to your problem would be Robocopy.
Robocopy is just plain awesome!
Here is the syntax of robocopy-
robocopy [Source] [Destination] [File] [...] [options]
Source
Specifies the source folder. Where you want to take the files from.
Destination
Destination directory/folder.
File
Here we are! This is what will help you. Here you can specify an extension you want to move. So in your case, your code would look somewhat like this.
robocopy *.jpg c:\destinationdir /S /MAX:1048576
*To execute this .bat every morning go to a program called task scheduler, dont worry, its built into windows. http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7
*Then Click on Create basic task, and set your task to whenever you like!
Thanks guys for your help!!!
I got it solved and there is a code below if someone would ever need something similar:
pushd Z:\intranet\PP Complete Images\
for /r %%a in (*.jpg) do (
XCOPY /Y "%%a" "C:\Users\username\Desktop\Master Image Folder"
)
popd

Resources