Problems with copying files from list via batch file - batch-file

I get a Text-file from an system - in this file there are filenames (per line one filename) - now with a batch-file every line (filename) from this list should be searched in a directory and copied in an other direcory.
I have this found and it almost works:
#echo off &setlocal
set "sourceRoot=C:\Users\test\Desktop\COPY_TEST\originale"
set "sourceList=C:\Users\test\Desktop\COPY_TEST\DBLIST.TXT"
set "destFolder=C:\Users\test\Desktop\COPY_TEST\kopiert"
for /f "delim=" %%i in ('dir /a-d /b /s "%sourceRoot%\*.pdf"^|findstr /ig:"%sourceList%"') do (
copy "%%~i" "%destFolder%\"
)
ECHO "Ausgabe abgeschlossen"
PAUSE
This works if the list looks like this:
filename1
filename2
....
BUT from the other system i get the list like this:
filename1;
filename2;
....
ANd now i have to edit the search term and add a specific term to them - original:
filename1;
But it should be searched for:
filename1_DE
So from the searchterm in the list the last character ";" should be cut and the term "_DE" should be added.
Not the list should be changed only the search term.
Is that possible?
Many thanks!

for /? explains expansion on variables, but in short, a file can be broken up into parts expanding on the metavariables. So as example if we have a string of a file:
D:\work\file1.txt
we can run a for loop to get the above string or parts of the string:
for %%i in (c:\work\file1.txt) do echo %%i
would echo D:\work\file1.txt
but we can now expand on %%i using things like drive, path, name, extension etc. So a straight forward example would be the below, you can copy it as is and save as a batch file and test it for yourself:
#echo off
for %%i in ("c:\work\file1.txt") do (
echo Full string and retain double quotes: %%i
echo Full string but remove double quotes: %%~i
echo Drive only: %%~di
echo Path only: %%~pi
echo Filename only: %%~ni
echo Extension only: %%~xi
echo Combination of drive and path: %%~dpi
echo Combination name and extension: %%~nxi
echo adding text between name %%~ni and extension %%~xi as: %%~niTEXT%%~xi
)
So given above explanation, you simply require using name only, add text and join with extension.
copy "%%~i" "%destFolder%\%%~ni_DE%%~xi

Thank you for your answer - i changed my version to following one and it works for me :)
#echo off &setlocal
set "sourceRoot=C:\Users\offic\Desktop\COPY_TEST\originale"
set "sourceList=C:\Users\offic\Desktop\COPY_TEST\DBLIST.TXT"
set "destFolder=C:\Users\offic\Desktop\COPY_TEST\kopiert"
set "sprache=_EN"
FOR /f "tokens=1 delims=;" %%i IN ('FINDSTR . "%sourceList%"') DO (
copy "%sourceRoot%\%%~i%sprache%.pdf" "%destFolder%\"
)
ECHO "Ausgabe abgeschlossen"
PAUSE

Related

How to pull specific data from comma separated .txt file and apply it to a variable to use in command line switch

I have hundrends of subfolders with identical file structures. One of the files in each folder is a metadata.txt file that contains comma separated information. I need to pull the 8th value and asign it to a variable %x and then use that variable as a switch in a follow-up command.
The metadata.txt file looks like this:
91,Chocolate,10,Large,Easy Bake Oven,350,0,39.9475,Cake,1.0.0.1,C:\CakeRecords,2044449,2(10-50),1,
Step 1: Assign varariable x (%x) the 8th value in the data. In this case it's 39.9475
Step 2: Run EasyBake command using the variable as a switch
Example: EasyBake.exe 39.9475
I'll be running this command one time in the root folder and having it go through every subfolder to run the command in each of them. I've already run a command to create a new folder 'NewRecipe' in each of the subfolders, so I've got a decent idea on how to bounce between folders to run the scripts, but any advice would be appreciated there as well.
FOR /d %A IN ("N:\old recipes\*") DO mkdir "%A\NewRecipe"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\metadata.txt"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%filename1%" '
) DO (
FOR /f "usebackqtokens=8delims=," %%t IN ("%%a") DO (
ECHO take your choice
ECHO PUSHD "%%~dpa"
ECHO MD newrecipe
ECHO easybake.exe %%t
ECHO POPD
ECHO ================
)
)
GOTO :EOF
Simply shows the required instructions.
Find each of the target files, get the appropriate field to %%t, then do what you will with the appropriate elements of %%a

findstr not working with string containing dash (-)?

I've been working on a BAT file which will delete old files based on creation date. To do this I've generated a list of all files and paths, then a list of files names to be protected. FINDSTR is then used to remove these files from the list of files and paths.
This system works fine until I encounter a file with a dash (or so it seems!)
Here's an example:
cleaner_protect.txt contains:
New File.txt
New File - Copy.txt
cleaner_fullpath.txt contains:
P:\New File.txt
P:\New File - Copy.txt
P:\Old File.txt
I want to remove the New Files stored in cleaner_protect.txt from the cleaner_fullpath.txt, leaving the Old Files behind which I will later delete (not up to that bit yet lol). Here is my code so far:
:: Remove protected files from list to be deleted (fullpath)
:RemoveFile
:: load string into variable
set /p target= <cleaner_protect.txt
:: remove protected file from full path list
echo -----------------------------
echo Searching for: "%target%"
echo -----------------------------
pause
findstr /v ".\<%target%\>." cleaner_fullpath.txt > cleaner_temp.txt
echo -----------------------------
type cleaner_temp.txt
echo -----------------------------
pause
del cleaner_fullpath.txt
ren cleaner_temp.txt cleaner_fullpath.txt
:: Count remaining lines in list
Set target=cleaner_protect.txt
Set /a lines=0
For /f %%j in ('Find "" /v /c ^< %target%') Do Set /a lines=%%j
Echo %target% has %lines% lines.
pause
:: Loop until completed
IF %lines% GTR 0 (
:: Remove line from protected list
more +1 cleaner_protect.txt > cleaner_temp.txt
del cleaner_protect.txt
ren cleaner_temp.txt cleaner_protect.txt
set /a lines-=1
GOTO :RemoveFile
)
Pauses and echos are for debugging purposes... I want this to run almost invisibly.
Can anyone shed some light on this? I need this code to repeatedly go through a dropbox and delete old files which may be in various levels of structure.
Maybe this simple line does all you want:
findstr /E /V /G:cleaner_protect.txt cleaner_fullpath.txt > cleaner_temp.txt
Sample output:
P:\Old File.txt
I would do it as follows:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem /* Prepend each line of `cleaner_protect.txt` with `\\`, remove a trailing space,
rem if applicable, and write result to `cleaner_temp.txt`: */
> "cleaner_temp.txt" (
for /F "usebackq delims= eol=|" %%E in ("cleaner_protect.txt") do (
set "ITEM=\\%%E|"
setlocal EnableDelayedExpansion
echo !ITEM: ^|=!
endlocal
)
)
rem /* Search `cleaner_fullpath.txt` for lines that do not end in any of the lines of
rem `cleaner_temp.txt` and process the returned items only: */
for /F "delims= eol=|" %%F in ('
findstr /V /I /E /L /G:"cleaner_temp.txt" "cleaner_fullpath.txt"
') do (
ECHO del "%%F"
)
rem // Clean up temporary file `cleaner_temp.txt`:
del "cleaner_temp.txt"
endlocal
exit /B
After having tested the script, remove the upper-case ECHO command.
Supposing cleaner_protect.txt contains this:
New File.txt
New File - Copy.txt
The temporary file cleaner_temp.txt is going to contain this:
\\New File.txt
\\New File - Copy.txt
So having the text file cleaner_fullpath.txt:
P:\New File.txt
P:\New File - Copy.txt
P:\Old File.txt
Only the following items are processed:
P:\Old File.txt
The leading \\ is taken as one literal \ by findstr (as it uses the \ as an escape character).
The prefix \\ is implemented in order to cover also the following situations:
Let us assume cleaner_protect.txt holds this:
New File.txt
And cleaner_fullpath.txt holds this:
P:\New File.txt
P:\Very New File.txt
Without the prefix, P:\Very New File.txt would also match New File.txt at the end, hence it would not become deleted erroneously.
Then let us assume cleaner_protect.txt holds this:
.File.txt
And cleaner_fullpath.txt holds this:
P:\.File.txt
P:\Other.File.txt
With a \ prefix, P:\Other.File.txt would also match \.File.txt at the end, because . is a meta character to findstr (even though literal search strings are defined by /L, but escaping like \. still applies and results in a literal .), hence it would also not become deleted erroneously. However, with a \\ prefix, escaping applies to the second \, so a literal \ is the result; the . does not need to be escaped with the /L option.

Batch Finding and displaying duplicate strings in file names

I have a question. Is it possibile in batch language to search in folder a part of name that is same like another file and display it.For example i got folder with files :
ggggggsss.mp3
ddddddeee.mp3
ddddddff.mp3
ssssssddd.mp3
aaaaasssss.mp3
11111ssdas.mp3
11111dddd.mp3
...
I need to display in cmd only names of files
ddddddeee
ddddddff
and
11111ssdas
11111dddddd
Because the first six letter are the same. Could someone help me with this problem?
Save this script to test.bat and run from open Cmd Prompt. Replace dir value with path to your folder with .mp3 files:
#echo off
setlocal enabledelayedexpansion
set "dir=%userprofile%\music"
set "pattern1=dddddd" & set "pattern2=11111"
pushd "%dir%"
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,6!"=="%pattern1%" echo %%G)
echo/
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,5!"=="%pattern2%" echo %%G)
popd
exit /b
See also Extract Substrings.

Batch file : how to get a specific part of a folder path

I'm currently looping threw the subfolders of a known directly. I would like to grab the folder that contains the file I'm looking for, but only that folder's name, not the whole path, and preferably in a different variable to be use later on in the batch file ...
pause
CD c:\%username%\testFolder\Program\OLD\
For /R %%G in (test.t?t) do Echo %%G
pause
Now, this shows me the file I'm looking for : C:\User\testFolder\Program\OLD\myfile\exemple1\test.txt
what I would like is to replace the Echo %%G to set in a variable the "unknown" folders and subfolder, something along the line of
For /R %%G in (test.t?t) do set var1 = myfile\exemple1
anyone can point to what I'm missing ?
If you for /? in a cmd console, you'll see runtime variables on the last couple of pages. Using %%~dpG notation you can get the full drive\path specification of matched files. Then, using substring substitution and delayed expansion, replace %CD% with nothing. Finally, you can strip the leading and trailing backslash with a numeric substring -- !varname:~1,-1!.
#echo off
setlocal enabledelayedexpansion
cd /d "c:\%username%\testFolder\Program\OLD\"
for /R %%G in (test.t?t) do (
set "var1=%%~dpG"
set "var1=!var1:%CD%=!"
if "!var1!"=="\" (echo .\) else echo(!var1:~1,-1!
)
If you wish, you can prevent echoing duplicates by echoing only if the previous match does not equal the current one.
#echo off
setlocal enabledelayedexpansion
cd /d "c:\%username%\testFolder\Program\OLD\"
for /R %%G in (test.t?t) do (
set "var1=%%~dpG"
set "var1=!var1:%CD%=!"
if not "!var1!"=="!prev!" (
if "!var1!"=="\" (echo .\) else echo(!var1:~1,-1!
)
set "prev=!var1!"
)

Batch cmd MKDIR folder with a name including symbol

I would like use command batch to create list folder.
But the name of folder include symbol characters, I don't know how to use cmd batch
Ex: I would like create list folder
01.DOCUMENT
02.SOURCE
03.DESIGN
04.TEST
05.REPORT
06....
Input: Input root-directory(strfolder)
Then example use: mkdir %strfolder%\ (+ name folder 01.Ducment,...) but I don't know use
My current code:
#echo off
cls
set /p folderName=Enter project name:%=%
#echo %folderName%
SET mypath=%~dp0
rem echo %mypath:~0,-1%
set folder=%mypath%%folderName%
if exist %folder% (
echo "Folder already exists"
) else (
mkdir %folder%
rem mkdir %folder%\%1 %RELEASE% --> this line don't know how :D
echo %folder%\%^1%
)
pause
You'd need to quote your target if it contains symbols:
mkdir "%folder%"
(md is the same as mkdir)
Also:
mkdir "%folder%" 2>nul
will create the directory; the 2>nul suppresses the error message.
%release% appears from nowhere. You don't say what it is or where it comes from; then you REM it anyway. No iea of what is happening there.
Now if your directory-names are in a file then
for /f "delims=" %%i in (filename.txt) do echo %%i
should show you the names. If you want to create subdirectories using these names then
for /f "delims=" %%i in (filename.txt) do echo MD "%folder%\%%i"
should do that for you - well, would ECHO the command; remove the ECHO keyword to actually make the directory.
Note that virtually any character would be happy so long as it remains in the metavariable %%i used as a loop-control. Some less-frequently-used characters can present a problem within an ordingary %variable%.
If your data contains parentheses, then the parser may become confused about whether a parenthesis is part of the command or the data. Best to avoid parenthesised constructs if that is the case.

Resources