This question already has answers here:
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 3 years ago.
I receive files via FTP that are placed in a folder called Landing. These files arrive in a folder that is randomly named, for example 000174, and the files inside are named Activity.txt.174 for example.
I have created a small script that allows me to reduce the file name to Activity.txt which works perfectly fine when executed within a command shell but when saved as a batch file it will not execute.
The script line is:
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
I have attempted to add various triggers from other scripts to get this working but when I get this working via CMD it still will not execute from a batch file.
Can anyone help please.
Mike
Change
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
to
FOR /R %%f IN (*.txt.*) DO REN "%%f" *.&
https://ss64.com/nt/for_f.html
Use double percent sign before your variable name.
Batch script
#echo off
FOR /R HERE_YOUR_ROOT_PATH %%f IN (*.txt.*) DO REN "%%f" *.&
#echo on
Related
This question already has answers here:
Delete all files and folders in a directory
(17 answers)
How to delete files/subfolders in a specific directory at the command prompt in Windows
(16 answers)
Closed 19 days ago.
I have a folder on my drive, which I regulary clean.
It has only txt files with sql logs.
How can I write batch script which is going to clean it when I run it ?
What I mean is, I do not want to go to drive, look for the folder, etc. Just have a shortcut on my screen, which is going to turn batch script on.
I tried to write it myslef, and modify it a bit, but its not working.
`#echo off
set "search_folder=C:\path\to\folder"
for /f "delims=" %%f in ('dir /b /s /a-d "%search_folder%\*.txt"') do (
type "%%f" | findstr /v /r "^[ ]*$" > temp.txt
move /y temp.txt "%%f"
)`
it does not delete anything
This is how you schedule a batch https://www.thewindowsclub.com/how-to-schedule-batch-file-run-automatically-windows-7
Code to delete bunch of files https://thegeekpage.com/how-to-delete-all-particular-files-inside-a-folder-in-windows-10/
This question already has answers here:
Touch file to update the date/time of file? (Modified Date) (WindowsXP BatchScript)
(3 answers)
Closed 2 years ago.
I want to develop batch having step that it will update timestamps of all files within some directory.
Below command gives list of all files in directories.
Can we redirect output of this command to some other windows equivalent touch command, so as to update the timestamps for all files?
cmd>dir /B
Log_adminUser_1.log
Log_adminUser_2.log
Log_adminUser_3.log
Log_adminUser_4.log
Log_adminUser_5.log
Log_adminUser_6.log
As a trick for touching all files in a directory, you could use this:
#ECHO OFF
FOR /F "delims=" %%G IN ('DIR /B') DO (
COPY /B "%%G"+,, "%%G" > NUL
)
The COPY /B construct is documented in TOUCH on SS64, which also explains some caveats with it.
This question already has an answer here:
Automating task for unzing a gz compressed file using windows batch script [closed]
(1 answer)
Closed 2 years ago.
I have two issues with this code I have attached below,
it doesn't delete the original file after unzip, I want the script to delete the original file from source folder
the converted file is not saved in target folder as set but it creates a tree of the directory in the same folder of the script and saved output there.
Please help me solve this issue
Sample code I have tried
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Please help me to write the script as described above
try: del file /f /q
try to enclose -o parameter with a quotes: "-o%target%"
Having troubles with cmd syntax, trying to delete files with specific extension in a specific folder that don't have sidecars. eg.: if folder contains:
1.A, 1.B, 2.A, 3.A, 4.A, 4.B
bat should only delete
2.A, 3.A
..hope that makes sense.
The code I've got so far must be real close, unfortunatelly not working
#echo off
FOR %%x IN (%1\*.A) DO
(
IF not exist "%1\%x.B" del "%1\%x.A"
)
Any help mostly appreciated.
Comments to question are correct
FOR variable percents must be consistently doubled when used in batch script
Open paren must be on same line as DO. But there isn't any need for parens with such a simple script.
Also, you want only the base name of the FOR variable, so you need the ~n modifier.
I made the code a bit more robust by using PUSHD at beginning.
#echo off
pushd %1
for %%F in (*.A) do if not exist "%%~nF.B" del "%%F"
popd
When I use the following code in command prompt, it works fine. But when I use the same code in a batch file, nothing happens. My batch file simply consists of:
for %f in (D:\flexcube1,D:\flexcube2,D:\flexcube3) do xcopy %f D:\o\ /e
but it does not work. I don't understand it and I have to use a batch file to copy multiple files. Any help would be really appreciated.
You need to use two % signs in for commands in a batch script (I know, who would have thought?)
for %%f in (D:\flexcube1,D:\flexcube2,D:\flexcube3) do xcopy %%f D:\o\ /e