DOS Batch file - Copy file based on filename elements - variables

I need to sort alot of files based on their filename. I would like to use a batch file to do it. I do know what I want but I am not sure of the correct syntax.
Example of filenames I work with: (They are all in the same directory originally)
2012_W34_Sales_Store001.pdf
2012_W34_Sales_Store002.pdf
2012_W34_Sales_Store003.pdf
2012_November_Sales_Store001.pdf
2012_November_Sales_Store002.pdf
2012_November_Sales_Store003.pdf
I would like to extract the information that are located between the "_" signs and put them into a different variable each time. The lenght of the informations contained between the _ signs will be different everytime.
Example:
var1="2012"
var2="W34" (or November)
var3="Sales"
var4="001"
If I am able to do this, I could then copy the files to the appropriate directory using
move %var1%_%var2%_%var3%_%var4%.pdf z:\%var3%\%var4%\%var1%\%var2%
It would need to loop because I have Store001 to Store050.
Also, there are not only Sales report, many others are available.
I hope I am clear.
Please help me realize this batchfile!

This script will make sure that it only attempts to move files that meet the pattern part1_part2_part3_part4.pdf
#echo off
for /f "eol=_ delims=" %%F in (
'dir /b *^|findstr /ix "[^_]*_[^_]*_[^_]*_[^_]*[.]pdf'
) do for /f "eol=_ tokens=1-4 delims=_." %%A in ("%%F") do (
move "%%F" "z:\%%C\%%D\%%A\%%B"
)
If needed, you could add md "z:\%%C\%%D\%%A\%%B" 2>nul before the MOVE in case the folders might not exist yet.

This script will move your files based on the values between the underscore to a like wise constructed path.
for %%f in (*.pdf) do call :handlefile %%f
:handlefile
set pad=z:
for /f "delims= tokens=1,* " %%a in ("%1") do call :step %%a %%b
rem this MOVES the file, maybe use echo first for testing
move "%fn%" "%pad%"
exit /B
:step
if !%2!==!! EXIT /B
set pad=%pad%\%1
for /f "delims=_ tokens=1,* " %%a in ("%2") do call :step %%a %%b
EXIT /B

Related

Set variables for each line of a txt file in a batch code

Is there a way to set each line of a txt document as a different variable in a batch file? I have got a txt file with all drives found on the computer. How can I set each line (for each drive) as a different variable, if this is possible at all?
I have a file which writes all avalible drives in a text file. I want to read this file out and set a different variable for each line of text. For example:
DeviceID
C:
D:
E:
Now "DeviceID" would be the first variable. "C:" "D:" and "E:" are all getting individual variables. I hope you get what I mean.
Thanks in advance!
Okay so I've tried this:
#echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
set /a i+=1
set drive[!i!]=%%a
)
set drive
pause
copy %0 %drive[1]%:\%random%.bat
pause
And it works pretty well. But I want to copy the currently executed file to all drives found without typing:
copy %0 %drive[1]%:\currentlyexecutedfile.bat
copy %0 %drive[2]%:\currentlyexecutedfile.bat
copy %0 %drive[3]%:\currentlyexecutedfile.bat
copy %0 %drive[4]%:\currentlyexecutedfile.bat
Is there a way to do this, without knowing how many drives there are and without causing errors?
Is there a way to do this, without knowing how many drives there are
and without causing errors ?
You should do it like this :
#echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
set /a i+=1
set drive[!i!]=%%a
)
Rem set drive
Rem If you don't know how many elements the array have (that seems is the case), you may use this method:
for /F "tokens=2 delims==" %%s in ('set drive') do (echo Copy %0 "%%s:\currentlyexecutedfile.bat")
pause

Batch - move files into alphabetical forders including unicode or 'strange' characters

I have some files and I want to move them inside alphabetical folder NOT previously created. With batch I want generate folders. Theses files must be moved inside these folders following files's first letter
I have a multi language file list inside my directory like this:
中文
alfa
35h
Ĕuid
لعربية
សេវិនខ្មែរ
I try this command to move files into alphabetic folders using first folder letter for ordering
#echo off
setlocal enabledelayedexpansion
for /d %%i in (*) do (
set first=%%i
set first=!first:~0,1!
md !first! 2>nul
if not "!first!" == "%%i" move "%%i" "!first!\%%i"
)
Nothing happens.
This part
for /d %%i in (*) do (
create folders and move folders and not files but I want move files inside generated folders (I don't want create folders previously)
You're currently using for /d which works on directories, not files. Since you have files, you need your for loop to work on them. If you just remove the /d, it will do so. However, note that this means it will also move the batch file itself. If you don't want that, then you'll need to put in logic to exclude it. Something like this:
#echo off
setlocal enabledelayedexpansion
for %%i in (*) do (
set first=%%i
if not "!first!" == "%0" (
set first=!first:~0,1!
md !first! 2>nul
if not "!first!" == "%%i" move "%%i" "!first!\%%i"
)
)
EDIT FOR ! SUPPORT
If the filename contains an exclamation mark, it won't work, because when delayed expansion is enabled, it sees that as part of a variable delimiter. The way to get around it is to assign the filename to a variable before you enable delayed expansion:
#echo off
setlocal
for %%i in (*) do (
set name=%%i
setlocal enabledelayedexpansion
if not "!name!" == "%0" (
set first=!name:~0,1!
md !first! 2>nul
if not "!first!" == "!name!" move "!name!" "!first!\!name!"
)
)

DOS Batch File Variable Modifier Returns Blank

I have a DOS batch file that will create a report listing files contained within a folder tree. The code below produces the desired output for over 115,000 files. However, 13 records are produced with blank date/time and file size. When I manually execute the DIR command (without the /b option), the desired file information is presented. Can this be corrected without adding considerable workaround code?
FOR /f "tokens=*" %%A IN ('DIR "<Path>" /a:-d /b /s') DO (
ECHO %%~tA %%~zA %%~dpA %%~nA %%~xA >> test.txt
)
(FOR /f "tokens=*" %%A IN ('DIR "<Path>" /a:-d /b /s') DO (
if exists "%%~A" ECHO %%~tA %%~zA %%~dpA %%~nA %%~xA
)) >> test.txt
The main reason for not obtaining a date/filesize is that the file can not be found.
How does your code work?
The for /f starts a separate cmd instance that runs the dir command.
When all the data has been retrieved and loaded into memory (that is, the cmd/dir command finished), then the for will start to iterate over the retrieved lines. Some time have passed between the information retrieval and the information processing.
In this time, "maybe" the problematic files have been moved/deleted/renamed and they can no be accessed to retrieve their properties. So, first check if the file still exists
The aditional parenthesis and redirection change are to avoid having to open the target file for each echo operation. This way, the file is opened at the start of the for command and closed at the end.

Removing everything after specific character in batch

SETLOCAL ENABLEDELAYEDEXPANSION
set "s=DIR D:\MyFolder /S /Q ^|FIND /i "Owner" ^|findstr /m /i "\.mkv$""
for /f "Tokens=5,6*" %%a in ('%s%') do (
SET _endbit=%%aa:*STRING=%
CALL SET _result=%%aa:%_endbit%=%%
>>%tmp%\list.txt echo %_result% %%b %%c
)
wscript "C:\my.vbs"
I am listing my files that owned by Owner and has extension mkv from MyFolder. I want to remove everything after specific character/word. I wrote that code. But It seems to be not working.
First of all, is it possible to do that? If so what is wrong with my code?
You came up with the same solution I was going to post on your other question - but I think you're missing some things. The line ending STRING=% is missing another % at the end, and the line %%aa:%_endbit%=%%, I'm not sure it can work directly on the variable from the for loop, and you need to store it in another variable first, and you probably need some expansions using ! characters too.
Here's what I had that seems to work, just as a test in a folder with three files in it, removing the end of the filename using E0 as the cutoff string:
SETLOCAL ENABLEDELAYEDEXPANSION
#echo off
for %%f in (*.mkv) do (
set FULLNAME=%%f
set ENDTEXT=!FULLNAME:*E0=!
call set TRIMMEDNAME=%%FULLNAME:!ENDTEXT!=%%
echo !TRIMMEDNAME!
)

Set file name with * as variable batch script

I have a batch file that i'm having issues with. I need to find the name of a file, then set it to a variable. Then I will use this to pass it onto a vbs script to further look into the file. The name of the file is logfile_date_time.log but the time varies depending on what time is starts. The point of the batch file is to find out the last modified date of this file.
set fordate=%date:~4,2%%date:~7,2%%date:~10,4%
set filename=c:\logfile_%fordate%_*.log
if exist %filename% (goto exist) else (goto noexist)
:exist
vbsscript.vbs /file:%filename%
goto end
:noexist
file doesn't exist code blah blah
:end
pause
I had to modify the names of folders and remove some code for security purposes since this is for work.
Any help appreciated. Thanks!
not tested:
set "last_modified="
for /f "delims=" %%f in ('dir /a-d /tw /o-d /b^| findstr /r /i /c:"logfile_[0-9][0-9]*_.log"') do (
do set "last_modified=%%~dpfnxf"
goto :break_loop
)
:break_loop
if defined last_modified echo file %last_modified% exist ...
The problem with your code is that it doesn't expand the wildcard character (*), and your VBScript probably doesn't handle wildcards in filenames by itself (the FileSystemObject methods for instance don't). If the file you want to process is the only one matching your pattern, you could do something like this:
#echo off
setlocal
set "fordate=%date:~4,2%%date:~7,2%%date:~10,4%"
pushd C:\
for %%f in (logfile_%fordate%_*.log) do vbsscript.vbs /file:"%%~ff"
popd