for /r not working with another for variable - batch-file

My script iterates through a list of user profile paths and copies a bunch of files into them. This works so far now I need to edit the copied .xml files so I thought I'll just add another for /r for these folders and do my thing. But I can't get this script working and I really don't know why. It's only entering the for /r loop when I substitute !PATH! with a constant value.
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%a in (users.txt) do (
SET PATH1=%%a\appdata\local\lazarus
for /R "!PATH1!" %%x in (*.xml) do (
call :fart "DKIUSER" "%%a" "%%x"
)
)
thats the content of my users.txt:
C:\Users\DKIUSER
C:\Users\dkiuser.DCJAN
C:\Users\ladmin

Related

Truncate specific files

I have several folders withing the same directory that are named like that :
001_Trial1
002_Trial2
003_Trial3
Trial4
Trial5
004_Trial6
005_Trial7
etc ...
I want to rename the folders in order to get
Trial1
Trial2
Trial3
Trial4
Trial5
Trial6
etc...
I tried to truncate it but the problem is that it will also delete the four first characters when the folder is "Trial3" it will rename it "3".
SetLocal DisableDelayedExpansion
For /D %%A In ("%1\*") Do (
Set "_d=%%~nxA"
SetLocal EnableDelayedExpansion
If Not Exist "%%~dpA!_d:~4!" Ren "%%A" "!_d:~4!"
EndLocal
)
I want all the outputs to start with "Trial"
The first thing I noticed looking at your code is, that you need to replace %1 with %~1 to get rid of the quotes.
I would then use a for /f to cut off the first part of your folder-names, so you don't need delayedExpansion and the code can handle folder-names with more or less digits at the beginning (and of course does not rename folders that already have the wanted name-format).
for /D %%D in ("%~1\*") do (
for /F "tokens=2 delims=_" %%N in ("%%~nD") do (
if not exist "%%~dpD%%~N%%~xD" ren "%%~fD" "%%~N%%~xD"
)
)

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.

Proper file locations

We have been having some issues where people have been putting files into the folder for the wrong job number. files for 664585_custnum_qty_filetype.dat should be in folder 664585 I am looking for a way to make sure that the files in a given folder all start with the foldername. I have been working with.
#ECHO OFF
FOR /R "V:\Work" %%G in (.) DO (
Pushd %%G
REM Echo now in %%G
for %%a in (.) do set currentfolder=%%~na
echo %currentfolder%
REM this is where I would be finding files that dont start the right way.
Popd )
Echo "back home"
Pause
trying to combine two other things that I have seen for iterating through folders and finding current directory name
Ultimately I want to list all the files that are out of place so that I can find them and figure out why they're not/who put them there. So I would like to have a program that iterated through all of the folders in V:\work into folders like 665485 and 332185 CustName displaying any files which do not start with 665485 or 332185 respectively.
I do have cygwin on my system, though not enabled all the time, and was considering ls, referencing C:\cygwin\bin\ls.exe directly to avoid it throwing off the normal functionality of some MS commands with the same name.
Any suggestions on how to get my list of files that dont start with the number from the foldername?
#echo off
for /d %%i in (*) do (if "%%i" NEQ "xArchive" (for /f %%j in ("%%i") do (dir "%%i" /b|for /f %%k in ('find "%%j" /v') do #dir "%%k" /b /s|find "thumbs.db" /v /i|find "xArchive" /v /i))
put this in the directory that contains the numbered folders and run it there. I'll be back in a few hours If you need me to change it. ;)

Batch find certain filenames then use the names in variable

I need help with a script that first finds all files in a directory with a certain string, then uses the filenames in a variable to be used in a script.
So:
Find files and filenames
Saves file?
Start some kind of loop? that changes a variable then executes the
belonging script
Repeat till all filenames have been used.
My code here..
#Echo off
For /r C:\work %%G In (*) Do #Findstr /M /S "string" > filenames.txt %%G
Set Var1=0
For %%G In (*) Do (
Var1=<filenames.txt (???)
script
script
I haven't writen "script" myself and friend help me with it, if you would like to see it do you need to wait until I can get to my other computer at home.
Thanks on beforehand!
Find files and filenames
Saves file
set "search=what I want to find"
(for /f "delims=" %%a in ('dir /a-d /b /s "C:\work" ^| findstr "%search%"') do echo (%%~fa)>filenames.txt
Start some kind of loop? that changes a variable then executes the belonging script
Repeat till all filenames have been used.
for /f "delims=" %%a in (filenames.txt) do (
REM here do something inside the loop
REM until all file names from filenames.txt were processed
)
This is designed to find files in c:\work that match a string, and echo the filenames.
#echo off
cd /d "c:\work"
for %%a in ("*string*") do (
echo "%%a"
)

How to set variable in batchfile

I want to move files according to folder names.
1.Some folder names have been written in b2.txt. In my b2.txt, every line contains one or two or three words, connected by space or "-". Like this:
transfer print
anti-foamer
insect
fibre reinforced plastic
2.My files are in "E:\JP-XIN\".
3.In E disk, there must exist one (only one) folder name consisting of one line in b2.txt.
My question is how to set every line in b2.txt exactly as variable.
In the following code, "%%k" is the file name gotten by searching, "%%l" is the path to the folder gotten by searching. The code did not work correctly.
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in (b2.txt) do (
set VAR=%%a
for /f "delims=" %%k in ('dir /s/b/a-d E:\JP-XIN\*.pdf E:\JP-XIN\*.txt ^| findstr /i /c:"!VAR!"') do (
for /f "delims=" %%l in ('dir /s/b/a:d-h E:\ ^| findstr /i /c:"!VAR!"') do (
if not "%%l"=="" move "%%k" "%%~fsl"
)))
pause
I had some time to spent so I worked overtime in how to solve what I think your problem is.
If your requirements are these:
The b2.txt file contains several folder names, with possible spaces.
In E:\ there is one folder that is contained in b2.txt.
In E:\JP-XIN\ there are several *.pdf and *.txt files.
and you want to:
Locate the folder that is contained in b2.txt, and
Move to it the *.pdf and *.txt files that have the same name of the folder
then the Batch file below solve your problem:
#echo off
for /f "delims=" %%a in (b2.txt) do (
if exist "E:\%%a" (
move "E:\JP-XIN\%%a.pdf" "E:\%%a"
move "E:\JP-XIN\%%a.txt" "E:\%%a"
)
)
If the .pdf and .txt are the only files with that name, then the two move commands may be joined in just one:
move "E:\JP-XIN\%%a.*" "E:\%%a"
If this is not your problem then, please, tell us what your problem is!
You need to use tokens=* to return the entire line as a single variable.
for /f "tokens=* delims=" %%a in (b2.txt) do (
You can also write things like tokens=1,2* which makes the first variable token 1, the second variable token 2, and the third variable the rest of the line. So the text:
Several words on a line.
Would split to:
%%a = Several
%%b = words
%%c = on a line.

Resources