Batch Script File Transfer with Variables - windows

Thank you for taking your time to help me with the problem I'm trying to solve. I have written some of the batch file but I don't have much experience so this a bit difficult for me. I'm trying to transfer pdf files from a specific location to individual folders in another location. Each file will contain this format
"GOOGLE EARTH_2018-08-07_5485A635.pdf" and based on "GOOGLE EARTH_" it will transfer it to it's rightful folder named as such "GOOGLE EARTH_Google Corporation". So based on the initial part of the file, transfer it to folder that begins with specified file name. This is what I have so far but doesn't work.
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Files"
SET "destdir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Transfer"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.pdf" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") ) DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%\*%%b*" '
) DO (
MOVE "%%a" "%sourcedir%\%%d\"
)
)
GOTO :EOF

In my answer I have aligned your code for extreme readability to show you how the closing parentheses lines up with each FOR command. I changed the MOVE command to use the source directory variable because the first FOR variable only holds the file name. Also changed the first IN clause to iterate PDF files.
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Files"
SET "destdir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Transfer"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*.pdf" ') DO (
FOR /f "tokens=1 delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN ('dir /b /ad "%destdir%\*%%b*" ') DO (
MOVE "%sourcedir%\%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF

Related

How to move file from another directory to created folder that make by name file?

I already make script file for make new folder base on file name and move 1 file to this folder. The code as below.
#ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion
set "sourcedir=C:\Users\XXX\4_MapIdentifyAndCSV"
set "destdir=C:\Users\XXX\5_OldData"
for /F "eol=| delims=" %%A in ('dir /B /A-D-H "%SourceDir%\*.csv" 2^>nul') do (
for /F "eol=| tokens=1 delims=." %%B in ("%%~nA") do (
md "%DestDir%\%%B" 2>nul
move /Y "%SourceDir%\%%A" "%DestDir%\%%B\"
)
)
endlocal
But I want to move another files from another directory to created folder too.
Example:
At C:\Users\XXX\2_Video move all file video to crated folder.
At C:\Users\XXX\Sub_Folder move all of Sub_Folder to crated folder.
And at C:\Users\XXX\3_Txt move all Text file to crated folder.
Please supporting me.
#ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion
set "sourcedir=U:\Users\XXX"
set "destdir=U:\Users\XXX\5_OldData"
FOR %%s IN ("4_MapIdentifyAndCSV:.csv" "2_Video" "Sub_Folder" "3_Txt") DO FOR /f "tokens=1,* delims=:" %%t IN (%%s) DO (
for /F "delims=" %%A in ('dir /B /A-D-H "%SourceDir%\%%t\*%%u" 2^>nul') do (
for /F "tokens=1 delims=." %%B in ("%%~nA") do (
ECHO md "%DestDir%\%%B" 2>nul
ECHO move /Y "%SourceDir%\%%t\%%A" "%DestDir%\%%B\"
)
)
)
GOTO :EOF
I use U: for testing.
The eol=| is not required in your code, so I removed it.
The extra "shell" using %%s..%%u works this way:
The quoted string from the list is assigned to %%s. Each entry in the list is "subdirectoryname:filemask". : is chosen because it can't appear in either a subdirectory name or a filemask. The for ... delims=:... assigns the subdirectory name to %%t and the filemask to %%u. If the filemask is not provided, %%u will be empty.
From there, it's just a matter of piecing together the appropriate parts of the filenames and directories required, inserting * before the filemask to specify "all".
In this way, you can control the filetypes moved if required. It would appear that you want only the .csv files from one directory and all files from the others. You could also specify "4_MapIdentifyAndCSV:.csv" "4_MapIdentifyAndCSV:.xyz" within the list to move all .csv and all .xyz files if you wanted that.
Please note that the md and move commands ar merely echoed for testing purposes. Remove the echo keyword from each of those lines to activate the create/move.
I am not sure that I understand the requirements.
By modifying the list so that each element is
source_subdir:destination_subdir:filemask
we can achieve
4_map...*.csv -> 5_olddata\filename\filename.csv
2_vid...* -> 5_olddata\dest...2_vid...*
sub_f...* -> 5_olddata\dest...sub_f...*
3_txt...* -> 5_olddata\dest...3_txt...*
Of course, the actual destination subdirectory could be common if required - all that would be required is that the second element becomes common.
The .csv destination for the 4_map... source is derived from OP's original code.
Note that the destination_subdir must be a single space as a sequence of delimiters is processed as a single delimiter (: = :: = :::::::)
#ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion
set "sourcedir=U:\Users\XXX"
set "destdir=U:\Users\XXX\5_OldData"
FOR %%s IN ("4_MapIdentifyAndCSV: :.csv" "2_Video:dest_for_2_video" "Sub_Folder:dest_for_sub_folder" "3_Txt:dest_for_3_text") DO FOR /f "tokens=1,2,* delims=:" %%t IN (%%s) DO (
for /F "delims=" %%A in ('dir /B /A-D-H "%SourceDir%\%%t\*%%v" 2^>nul') do (
IF "%%u"==" " (
rem if second element of list item is "empty", destination is directory ...\filename
for /F "tokens=1 delims=." %%B in ("%%~nA") do (
ECHO md "%DestDir%\%%B" 2>nul
ECHO move /Y "%SourceDir%\%%t\%%A" "%DestDir%\%%B\"
)
) ELSE (
rem if second element of list item is not "empty" it is the destination directory ...\%%u
ECHO md "%DestDir%\%%u" 2>nul
ECHO move /Y "%SourceDir%\%%t\%%A" "%DestDir%\%%u\"
)
)
)

Change the names of pictures in a folder using a text file

I have several pictures of my students in a folder, and a list with their names in a text file.
I would like to creat a batch file to rename the pictures using the text file (names.txt) so that every picture has the name of the student.
All the pictures are in .png format. I searched this site and tried the following code :
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem Load the list of new filenames
set i=0
for /F "delims=" %%a in (names.txt) do (
set /A i+=1
set "newname[!i!]=%%a"
)
rem Do the rename:
set i=0
for /F "delims=" %%a in ('dir /b /o:n *.png') do (
set /A i+=1
for %%i in (!i!) do ren "%%a" "!newname[%%i]!"
)
I creat the batch file in the folder and when I execute it, there is nothing happening.
I think it is not picking the right folder to work into, but I'm not sure.
Example of files:
1.png
2.png
3.png
Example of names.txt
1_john_dalton
2_carol_denvers
3_steve_austin
Based on your comments and the construction of the current code, it seems you want something like this:
#echo off
for /F "delims=" %%a in (names.txt) do (
for /f %%i in ('dir /b /o:n *.png') do echo ren "%%i" "%%a%%~xi"
)
This above will simply echo the result and not do the rename, only if you are happy with the results, can you remove echo from the last line, before ren.
Your example seems to want to resuse the old numeric name as well, so if indeed the case, then this should be it:
#echo off
for /F "delims=" %%a in (names.txt) do (
for /f %%i in ('dir /b /o:n *.png') do echo ren "%%i" "%%a[%%~ni]%%~xi"
)

Batch for scanning dirs needs a possibility to skip defined dirs

I have a batch which scans a directory structure and gives out the size of directorys I defined he should look for.
E.g. the directory '10XXX' contains the subdirectorys '10001', '10002' and '10003'. Now lets say that the batch should look for the dir 'XYZ' in those subdirs. Result is a .csv with informations like '10001\XYZ' 100 Bytes; '10002\XYZ' 2000 Bytes; '10003\XYZ' 0 Bytes.
The problem is the batch scans every directory every time it runs, which takes a lot of time and resources.
My idea is to implement a section into the code that everytime the batch starts scanning a dir it takes a look into a .txt (for example) and when the dir name is found in that file the batch skips the dir.
This is maybe not the most effective solution, but it will take less time and resources than scanning every dir every time.
So different ideas are of course welcome.
Unfortunately I have not enough knowledge to do it myself so I hope that someone could help me.
Code:
#echo off &setlocal
set /p rootfolder=<Enter_Directory_Path_Here.txt
set /p savefolder=<Enter_Save_Directory_Here.txt
set "batpath=%~dp0"
pushd "%rootfolder%"
for /d %%i in (*) do (
set "foldername=%%~nxi"
set "folder=%%i"
>"%batpath%%%~nxi.csv" type nul
for /f "delims=" %%j in ('dir /ad /b "%%i\*"') do (
set "subfolder=%%j"
call :procfolder
)
>"%batpath%%%~nxi.~csv" type nul
for /f "usebackq tokens=1* delims==" %%j in ("%savefolder%\patterns.txt") do (
>>"%batpath%%%~nxi.~csv" findstr /b %%k "%batpath%%%~nxi.csv"
if errorlevel 1 >>"%batpath%%%~nxi.~csv" echo "%%j";"not found"
)
>nul move /y "%batpath%%%~nxi.~csv" "%batpath%%%~nxi.csv"
)
popd
call :cleaner
exit /b
:procfolder
setlocal
for /f "tokens=3" %%i in ('dir /a /-c "%folder%\%subfolder%\"^|findstr /c:"Datei(en)"') do >>"%batpath%%foldername%.csv" echo "%subfolder%";%%i;"Bytes"
for /f "delims=" %%i in ('dir /ad /b "%folder%\%subfolder%\"') do (
set "subfolder=%subfolder%\%%i"
call :procfolder
)
endlocal
exit /b
A sample of reading a text file to match
for /F "tokens=*" %%X in (N:\SomeDirectory\SomeFile.txt) do if /I "%foldername%" EQU "%%X" goto SKIPIT
REM Do your normal stuff here
:SKIPIT
If the exclusion filename is encapsulated in quotes, add the usebackq option:
for /F "usebackq tokens=*" %%X in ("N:\Some Directory\Some File.txt") do if /I "%foldername%" EQU "%%X" goto SKIPIT
REM Do your normal stuff here
:SKIPIT

Windows batch operation for adding pdf page dimensions to filename?

I need to include page size information of many single-paged pdf's into their filenames. E.g. "150x250mm.pdf". I found no renamer apps able to do it. I suspect this could be done using a batch file and pdfinfo.exe (from xpdf suite), but I have no idea how to make use of it.. Could you give me some hints?
Yes, you can convert from postscript points to MM. In this case, the script is in the top level folder containing the PDF's to be renamed. It does go into subfolders. If you don't want or need that, remove the /s from the dir command on the 5th line. Change the paths as needed.
#echo off
setlocal enabledelayedexpansion
set "pdfi=U:\Scripts\Utilities\xpdf\pdfinfo.exe"
for /f "delims=" %%a in ('dir /b /s *.pdf') do (
for /f "tokens=3,5 delims= " %%b in (
'%pdfi% -meta "%%a"^|find /i "Page size:"') do (
set pts=%%b %%c
for %%d in (!pts!) do (
call :Eval %%d*.352777778 mm
set "mm1=!mm1!x!mm!"
)
ren "%%~dpfnxa" "!mm1:~1!.pdf"
set mm1=
)
)
exit /b
:Eval <in> <out>
setlocal
if exist eval.vbs del eval.vbs
>eval.vbs echo wsh.echo formatnumber(eval("%1"),0)
for /f "delims=" %%a in (
'cscript //nologo eval.vbs'
) do endlocal & set %~2=%%a
del eval.vbs

How to bulk add folder name to file name?

I have a folder struktur like this:
/1/1/master.jpg
/1/2/master.jpg
/1/3/master.jpg
/2/1/master.jpg
/2/2/master.jpg
/2/3/master.jpg
...
I need to import all images to a website, but the file name have to differ to each other, so I cannot import two (or) more files with the same name. Just numerating the images to master1.jpg, master2.jpg, ... with e.g. AntRenamer is no proper solution, because the image paths/names are assigned to an item number in a csv file I also need to import.
So: How can I bulk add the folder names to the files like this?
/1/1/1_1_master.jpg
/1/2/1_2_master.jpg
/1/3/1_3_master.jpg
/2/1/2_1_master.jpg
/2/2/2_2_master.jpg
/2/3/2_3_master.jpg
...
Thanks for your help!
Timo
#echo off
setlocal EnableDelayedExpansion
cd C:\Parent\Folder\OfFirstNumberedFolders
for /F "delims=" %%a in ('dir /B /S /A-D master.jpg') do (
set "fullName=%%a"
for /F "tokens=1-3 delims=\" %%b in ("!fullName:%CD%=!") do (
ECHO ren "%%a" "%%b_%%c_%%d"
)
)
Try this in Windows. Remove the echo to make it actually perform the rename.
#echo off
for /f "delims=" %%z in ('dir "master.jpg" /b /s /a-d ') do (
for %%a in ("%%~dpz%\.") do (
for %%b in ("%%~dpa\.") do (
echo ren "%%z" "%%~nxb_%%~nxa_%%~nxz"
)
)
)
pause

Resources