Handling space in the file name, FOR loop, batch - batch-file

I am trying to find all the MP3 files in a given directory, then export it to a file, before trying to use the contents of the file as an input.
All the instances of file copy are working fine, except for the file locations which contain spaces. How should I address this in my current code. Please refer to the screenshot below
Contents of my MP3_Location.txt file are:
C:\Test\asdad.MP3
C:\Test\New folder\werwer.MP3
C:\Test\OneDrive - Backup\asdasdasdad.MP3
REM Exporting the location of the MP3 file in a given directory
DIR /s/b "C:\Test\*.MP3" >> C:\Software\MP3_Location.txt
REM Trying to copy the files based on the previous Output
FOR /F %%G IN (C:\Software\MP3_Location.txt) DO c:\windows\system32\xcopy
"%%G" C:\Software\MP3\ /Y
Edit 1: Trying to use Delims now, as suggested (perhaps not using it correctly)
REM Exporting the location of the MP3 file in a given directory
DIR /s/b "C:\Test\*.MP3" >> C:\Software\MP3_Location.txt
REM Trying to copy the files
FOR /F %%G "tokens=* delims=" IN (C:\Software\MP3_Location.txt) DO c:\windows\system32\xcopy "%%G" C:\Software\MP3\ /Y

Used the command
FOR /F "delims=" %%G IN

Related

organise files based on words in the file and create a directory and put them in there

I have about a few thousand files in a folder. Each filename has a few different version's of the file so for example. tree.blue, tree.green, tree.orange, cat.black, cat.green,cat.white.
what i would like to do is have a .bat file to put all tree or any word before the "." to auto create a directory and add any file related to tree for example in it as well as for all the other filenames without specifying the word.Currently I have found code on here which almost does it but have to specify the keyword in the code for it to work.
Assuming each file is in the same format, something like this would work:
pushd "path_to_folder"
for /f "tokens=1* delims=." %%g in ('dir /b') do (
if not exist "%CD%\%%g" #echo md "%%g"
#echo copy "%CD%\%%g.%%h" "%CD%\%%g\%%g.%%h"
)
popd
You can remove the #echo once you confirm the system will be creating the right directory name and copying the files to the right location.

Batch File - Using List, move files from folders

I have 3 folders located in a INPUT folder.
Also have 3 folders in a OUTPUT folder like so...
"INPUT"
-Folder1
-Folder2
-Folder3
"OUTPUT"
-Folder1
-Folder2
-Folder3
I have a file listing (list.txt) from INPUT\Folder1.
I need it to read each line from the list.txt, and if exists as a file, then move those to the OUTPUT destination folders respectfully.
Here's what I have right now, but it copies all files over to the destination folders and not just what is in the list.txt:
for /f %%f in (%CD%\list.txt) do robocopy %CD%\INPUT\folder1
%CD%\OUTPUT\folder1
for /f %%f in (%CD%\list.txt) do robocopy %CD%\INPUT\folder2
%CD%\OUTPUT\folder2
for /f %%f in (%CD%\list.txt) do robocopy %CD%\INPUT\folder3
%CD%\OUTPUT\folder3
I figured it out!
Decided to try xcopy and throw in the "delims=*" for spaces and special characters in the filenames and it worked!
For /f "delims=*" %%f in (list.txt) do xcopy "%CD%\INPUT\folder1\%%f"
%CD%\OUTPUT\folder1
So now it will only copy over the files listed in the list.txt.

batch script move files zero bytes same name

Couldn't find a way to do this, looking to store the names of all zero byte files from a folder in a list.
if exist *.xml for %%i in (*.xml) do if %%~zi==0 ECHO "%%i" >> list.txt
this works but the next step would be to read the list.txt and move all files with the same file name even if it has a different extension, also the other file isn't zero bytes that has the same file name but different extension.
example 12345.html and 12345.xml - both files would be moved because the name is the same.
any help would be amazing!
thanks
look at the modifiers in for /?. For example %%~nA would give you the name without the extension.
for /f "delims=" %%A in (list.txt) do (
echo ---- %%nA ---- files with same basename:
dir /b "%%~dpnA.*"
)

Batch Script Move file to directory based on its current directory

I'm attempting to sort a lot of files based on the current location of the file e.g.:
File 1 is located at C:\Work\Movies\Subs\Subtitle.txt
File 2 is located at C:\Work\Movies\Subs\Special\Subtitle.txt
File 3 is located at C:\Work\MoviesSpanish\Subs\Subtitle.txt
I'm trying to move the files like so:
File 1 to C:\Work\InProgress\Movies\Subs\Subtitle.txt
File 2 to C:\Work\InProgress\Movies\Subs\Special\Subtitle.txt
File 3 to C:\Work\InProgress\MoviesSpanish\Subs\Subtitle.txt
The Batch Script is to be located in C:\Work\MoveFile.bat
There are away more files then I listed above. Just for an estimate I would say around 300-500 per folder and there's a lot more subdirectories (e.g. .\Subs\01\ all the way up to .\Subs\300\ and they each contain a bunch of text files). I need to move all of the .txt files from their current locations to a new folder in C:\Work\ while retaining the rest of the directory location. So they get moved to C:\Work\[New Folder]\[Rest of Original Location]
I want to do this in batch but I'm not sure where to start. I already have the following code, which deletes files that don't contain a specific string:
for /r %%Z in (*.txt) do (
SET "count="
for /F "usebackq delims=," %%A in ("%%Z") do (
if /i "%%A"=="LN" set count=1
)
if not defined count echo del "%%Z"
if not defined count del "%%Z"
if defined count move "%%Z"
echo %count%
echo %%Z
)
But I'm not sure how to obtain the correct directory to move them into. I was thinking about for loop that reads the directory string of the file in question and uses delims=/ but it kept reading the text file rather then the path (and when I didn't use quotes on %%Z, it decided it couldn't find the file).
This is untested - it uses robocopy to move the *.txt files into a new location which is outside the source folder (because of cyclic copy restrictions) and then moves them back to the new "C:\Work\InProgress" folder.
If %temp% and c:\work are on the same drive then it should be swift.
Change c:\work in both places to a test folder with a few copies of your files and test it.
#echo off
robocopy "C:\Work" "%temp%\temporarymoviefolder" *.txt /s /mov
robocopy "%temp%\temporarymoviefolder" "C:\Work\InProgress" *.txt /s /mov
pause

Need Batch script to search and copy specific file from network drive

I am very new to batch scripting.
I need to write a batch script to search for a specific file(usually .zip or .7z extension) located on network drive directory(containing multiple folder and sub-folders with space in name) and copy the same to my local drive.
Also I need to copy a zip file containing "elf" keyword which will also be located in the same directory where my file is present.
For example:
Search file: abc.zip
Network drive:\abc.com\test
So basically I need to search for my file abc.zip in the network directory(including sub-folders) and if found copy the same to my local drive(say c:\Temp) and also I need to copy *elf* file to the same local directory.
Thanks in Advance.
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure paths
set "source=\abc.com\test"
set "target=c:\test"
rem Change drive/path to source of files
pushd "%source%"
rem Recurse folders searching adecuated files and when found, copy to target
for /f "tokens=*" %%f in ('dir /s /b "abc.*z*" ^| findstr /i /e /c:".zip" /c:".7z"') do (
copy /y "%%~ff" "%target%"
if exist "%%~dpf\*elf*.zip" copy /y "%%~dpf\*elf*.zip" "%target%"
)
rem Return to previous drive/path
popd
rem Clean
endlocal
This will search the source folder hierarchy for indicated filename and an aproximate match based on file extension (.*z* matchs .zip, .7z and more), filters file extensions to only accept the cases needed (.zip and .7z) and copy the required files to target folder.
Have you considered using the Extended Copy Utility (xcopy)? Syntax is as simple as
xcopy "<directoryToCopyFrom>" "<directoryToCopyTo>" /C /S /D /Y /I
This will work assuming you're wanting to write a Windows batch script.
Searching for the "elf" string will be a bit trickier though. You might consider doing that using Java, and then call the batch file from the Java program.
for /d /r "drive:\abc.com\test" %%A in (*) do (
if exist "%%~A\abc.zip" copy "%%~A\abc.zip" "C:\Temp"
if exist "%%~A\*elf*" copy "%%~A\*elf*" "C:\Temp"
)

Resources