Using batch commands to create folders and copy filenames - batch-file

I want to copy all files on a list to another location and create sub-folders in that location based on certain attributes of the filenames. In this case, the company names. All files are currently located in desktop\main.
For example, lets say the list has filenames like
From((Sales#JonesCompany.com))main.txt
From((AR#PeterIndustries.com))main.txt
From((AP#BaseCorporation.com))main.txt
The script should copy those files, make a directory like JonesCompany.com, and finally copy all respective filenames to that location.
The end result should be like
desktop\final_location\JonesCompany\
desktop\final_location\PeterIndustries\
desktop\final_location\BaseCorporation\
So far I have
for /f "delims=" %%i in (main.txt) do echo F|xcopy "C:\Users\Desktop\Main\%%i" MD %%i "C:\Users\Desktop\final_location\%%i" /i /z /y
Thanks in advance

I think this does what you want:
#ECHO OFF
SETLOCAL
set "basepath=C:\Users\Desktop\Main\"
set "filter=*.txt"
set "targetpath=C:\Users\Desktop\final_location\"
echo scanning %basepath%%filter%
for %%i in ("%basepath%%filter%") do (
for /F "tokens=1,2,3 delims=#)" %%a in ("%%i") do (
echo Creating Folder %%b...
mkdir %basepath%%%b\
echo copying file %%i to %targetpath%%%b\...
copy "%%i" %targetpath%%%b\
)
)
You can easily change the basepath, targetpath and filter variables as needed.
Explanation
The trickiest part is for /F "tokens=1,2,3 delims=#)" %%a in ("%%i") do (. %%i is a filename. Let's say it's From((Sales#JonesCompany.com))main.txt. This for statement will split the string at any instance of the delimiters # or ). This leaves us with the following array of strings:
%%a: "From((Sales"
%%b: "JonesCompany.com"
%%c: NULL (I think)
%%d: "main.txt"
Obviously, we want %%b so we use it as the new folder name. We then copy %%i, the original full path, to the new folder.
update removed a loop thanks to #Stephan pointing out something I didn't realize!

Related

Moving files with a batch file

I have 30 folders, one for each travel group with 10 subfolders each, each subfolder corresponds to an activity.Some pictures dont match with the group, so i have a list of the pictures that dont match.
Im using this batch to move the jpgs from the folders but it's putting them all together. is there any way that the batch creates a folder with the name of the folder it's been moved from?
for /r "originfolder" %%# in (*) do findstr "%%~nx#" "filelist.txt"&&move "%%#" "destinyfolder"
PAUSE
You'll need a few more lines. There are a few different ways, this being one of them.
#echo off
setlocal enabledelayedexpansion
pushd "originfolder"
for /F "usebackq delims=" %%a in ("filelist.txt") do for /F "delims=" %%i in ('dir /b /s "%%a" 2^>nul') do (
set "filepath=%%~dpi"
set "filepath=!filepath:~0,-1!"
for %%f in (!filepath!) do set "destpath=%%~nxf"
mkdir "C:\Full path to\destinypath\!destpath!"
move "%%~a" "C:\Full path to\destinypath\!destpath!"
)
popd
simply get the path of the file, then use the last name of the path (folder where file exists) and create that folder name in your destination path, before moving the file.
Note, this is untested code, so please use a test scenario before attempting this in production.

Loop through folder with files including a space

I want to loop through a folder and let run an algorithm on each .tif file found. Unfortunately this does not work for files which have a space character in their name. As my path already contains folders with space, i put the variable which stores the path name in double-quotation marks.
FOR /F %%k IN ('DIR /B "%mypath_import%"*.tif') DO (
SET infile=%%k
SET outfile=!infile:.tif=_UTM.tif!
REM ... do something
This is my attempt so far but it won't work for the files which include a space as well.
You done need all that. You can use the normal for loop without having to use /f
#echo off
setlocal enabledelayedexpansion
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do (
set "infile=%%~i"
echo "!infile:.tif=UTM.tif!"
)
The above will however echo full path to and file name, if you want filename only with extension:
#echo off
setlocal enabledelayedexpansion
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do (
set "infile=%%~nxi"
echo "!infile:.tif=UTM.tif!"
)
or without the need to delayedexpansion
#echo off
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do echo %%~dpni_UTM%%~xi
and again if you require the name and extension only.
#echo off
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do echo %%~ni_UTM%%~xi
EDIT
As per comment from #Stephan, keep in mind if you are doing actual renames and you run the script more than once it will keep on appending _UTM each time. So you'll get filename_UTM_UTM.tif etc. So you can exclude files from the loop by including findstr
for /f "delims=" %%i in ('dir /b *.tif ^|findstr /eiv "_UTM.tif"') do echo %%~ni_UTM%%~xi

Find, Copy and Rename in BATCH

Good Morning!
Coming from another article, which i tried to adopt, but failed, i would like to ask my question in a separate post. I need to achieve two things:
Copy all files that contain a certain string in their filename from one directory to another, but only if that file does not already exists in that target directory. The filename could be something like EventLog_12345.txt and i would want to copy only the files where the filename contains EventLog.
In a set of files identify in every .txt file a certain string. This string indicates the line that contains the string i am looking for. I want to get to the end of this line and save the .txt file as a new .txt file with a new name based on the string i find at the end of this line. Example: My file is EventLog_12345.txt and somewhere in this file there is a line like this:
2018-06-22 08:21:19 0133 LET vVariable = 'h**ps://somedomain.com/test/1/2/4/jobs/joblog.XML'
The string indicating the line is vVariable.
The string i want to use within the new filename in this example is joblog.xml. The file should be stored as a new .txt file with the name: joblog_12345.txt. Note, that the length of the line can vary; so can the length of the domain string; also the names of the XMLs are different. The constant is that i always
want to have the name of the XML file which is always the last piece of the domain.
Adding info on efforts so far
Copy & Paste - this is actually working, but does not check whether a file already exists:
#echo off
for /f "delims=" %%a in (
'xcopy /l /e /y "\\myPath\*EventLog*.txt" "D:\Target\" ^|find "EventLog"'
) do copy "%%a" "D:\Target\"
For the identification of string and then SaveAs i dont really have anything. I was basically hoping i could somehow adjust the solution provided here: (Rename text files based on multiple strings in their contents)
For 1st
#echo off
Set "Target=D:\Target\"
for /f "delims=" %%A in (
'xcopy /l /e /y "\\myPath\*EventLog*.txt" "%Target%" '
) do if not exist "%Target%%%~nxA" copy "%%A" "%Target%"
For 2nd
process a list of files containing vVariable with findstr /I /M "vVariable" *_*.txt in %%A
Split the found name at the underscore %%B
search file to get the Line and exchange all / in the line to spaces to then
get the very last element in %%D
use %%~nD without extension to form the new name
:: Q:\Test\2018\05\22\SO_50982243.cmd
#Echo off & Setlocal EnableDelayedExpansion
For /f "delims=" %%A in ('findstr /I /M "vVariable" *_*.txt ') Do (
For /f "tokens=2delims=_" %%B in ("%%~nA") Do (
For /f "delims=" %%C in ('findstr /I "vVariable" ^<"%%A"') Do Set "Line=%%C"
Set "Line=!Line:/= !"
For %%D in (!Line!) Do Set "NewName=%%~nD_%%B%%~xA"
Echo Ren "%%~A" "!NewName!"
)
)
Sample output based on your information
> SO_50982243.cmd
Ren "EventLog_12345.txt" "joblog_12345.txt"
If the output looks OK remove the echo in front of ren to really rename.

batch file to copy files based on their names

I am trying to come up with a batch file to copy files based on their names.
The filenames are in form aaa_xx-XX-ccc.txt where aaa and ccc do not matter (variable length), but xx are 2 character codes. I would like to copy the files into their respective xx folders and these folders would also be created by the script.
Here I found something that almost suit my needs:
Batch file to copy files based on characters in filename
#echo off
setlocal enableextensions enabledelayedexpansion
for %%x in (*.txt) do (
set "filename=%%x"
set "folder=!filename:~1,2!"
if not exist !folder! mkdir !folder!
copy "%%x" !folder!
)
But I would like to be looking for the xx not only by its position in the filename, but by the specific character (underscore) and position in the filename.
Is there any way to do it?
Update - example:
I have the following files in a single folder:
My-Project_ar-SA-2016114-12h33m10s.txt
My-Project_cs-CZ-2016114-12h33m8s.text
My-Project_da-DK-2016114-12h33m10s.txt
The "template" for the filenames is "[project-name]_[language-CODE]-[dateAndTime].txt
And I want to copy the first file into a different location\ar folder (for example "C:\Output\ar"), the second file into "C:\Output\cs" etc.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*_*-*-*.txt" '
) DO (
FOR /f "tokens=1*delims=_" %%c IN ("%%a") DO (
FOR /f "tokens=1,2*delims=-" %%q IN ("%%d") DO (
ECHO(COPY "%sourcedir%\%%a" "%destdir%\%%q\"
)
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)
It isn't clear whether you want the destination directory to be xx or XX. This is set up for xx for XX use %%r in place of %%q in the copy line.
Read the directory for files only using the mask _--.txt into %%a
Remove the leading characters up to the underscore by tokenising using _ and selecting %%d, the trailing portion
re-tokenise %%d on -. first token to %%q, second to %%r
Mix and match to create the copy command.
Note that your second filename, ...CZ... is a .text filename, not a .txt
According that all .txtfiles in the directory you're running the BAT are to be copied and the template is stricly what you described.
Here is an idea to do it :
#echo off
for /f "tokens=1,2,3* delims=_-" %%a in ('dir *.txt /b/a-d') do echo copy "%%a%%b%%c%%d" "c:\output\%%c"
Remove the echo before the copy if the Output is OK

Nested for loops and looping through subfolders, is this legal? .bat script

I am trying to get a feel for .bat files and I am trying to modify a script so it can loop through every subfolder in a directory and delete certain file types within each folder.
Here is an idea of what I currently have and I would like to know if it is legal or not to do something like this:
*Creates "deletethese.txt" via sql command*
FOR /F "tokens=1 delims=," %%v IN (deletethese.txt) DO (
For /R C:\Users\Public\Documents\ %%G IN (%%V) DO ECHO Deleting File %%G
For /R C:\Users\Public\Documents\ %%G IN (%%V) DO DEL "%%G"
)
Basically it is taking the txt file it generates from the sql command and loops through it and takes it's contents. I need this script to go through each subfolder in the directory and check if those contents exist and if they do, echo that variable and then delete that content. Also, do I need the "%%G IN (%%V)" or can I leave out the parameter and just use %%V.
I am not sure if this is the best way to go about this or not or if I am even on the right track, that is why I am here.
Thanks for any help! Much appreciated!
I need this script to go through each subfolder in the directory and check if those contents exist and if they do, echo that variable and then delete that content.
As I understand it, you just need to confirm a filename specified in deletethese.txt exists in a folder (C:\Users\Public\Documents) and if so, delete it.
That being the case, you can accomplish this with the following FOR loop:
*Creates "deletethese.txt" via sql command*
SET "PathToCheck=C:\Users\Public\Documents"
FOR /F "tokens=1 delims=," %%v IN (deletethese.txt) DO (
REM Verify file exists before attempting to delete.
IF EXIST "%PathToCheck%\%%v" (
ECHO Deleting File %%v
DEL "%PathToCheck%\%%v"
)
)
However, if you need to locate files with the specified name within the specified folder, you can do this:
*Creates "deletethese.txt" via sql command*
SET "PathToCheck=C:\Users\Public\Documents"
FOR /F "tokens=1 delims=," %%v IN (deletethese.txt) DO (
REM Locate all files with the name within the path.
FOR /F "usebackq tokens=* delims=" %%A IN (`DIR "%PathToCheck%\%%v" /B /S /A:-D`) DO (
ECHO Deleting File %%A
DEL "%%A"
)
)
Actually, your original code was very nearly correct.
Batch is largely insensitive to character-case except for the metavariable (loop-control variable) %%v/V in your case.
So all you'd need to do was make your %%vs all the same case.
As a By-the-by,
For /R C:\Users\Public\Documents\ %%G IN (%%V) DO ECHO Deleting File %%G
is better as
For /R "C:\Users\Public\Documents\" %%G IN (%%V) DO ECHO Deleting File %%G
because the quotes would need to be there if the directory-name contained Space or other separators.
And... your original would have listed all the files, then commenced deleting them; possibly better as
For /R C:\Users\Public\Documents\ %%G IN (%%V) DO ECHO Deleting File "%%G"&del "%%G"
where & is the inline statement-separator.

Resources