I am new to programming, thanks to the new task my boss has provided.
I am trying to run a batch file to zip multiple files in a folder separately.
So, I want file1.txt to zip to file1.zip and so on for other files.
I have only the following code:
for /f "tokens=*" %f in ('dir /b *.DAT') do "c:\Program Files\7-Zip\7z.exe" a "%f.7z" "%f"
My issue: When I run it on cmd after navigating to the target folder, it works, but when I store it in a batch file and run it from the target folder, it wont work.
Please help me identify what and where I need to make changes in my code.
Regards
AK
WIthin a batch file, the metavariable (loop-control variable) requires the % to be doubled, so replace each %f with %%f when you mean the loop-control variable"f"`
Your code seems fine. The issue is, that when you run it inside a batch file, you need to put an extra % mark.
In the batch try:
for /f "tokens=*" %%f in ('dir /b *.DAT') do "c:\Program Files\7-Zip\7z.exe" a "%%f.7z" "%%f"
Related
I am trying to find specific files within a folder and its sub-folders and copy them to a new folder. I used to use this batch file for this purpose but now I get this error :
The system cannot find the file specified.
Here's the batch-file content:
pushd "\\internal.company.com\path\"
md myfile
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do copy %%G "\\internal.company.com\path\myfile"
Any input is appreciated.
UPDATE
I tried printing %%G like this:
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do echo %%G
and it works well. The problem arises with copy command which cannot read %%G as an argument.
I'd change to
... copy "%%G" ...
which would cater for spaces in %%G.
Perhaps you should determine whether the problem is with the for/r or %%G. If you simply display %%G with an echo %%G command, then you may see some problem. If it still won't run, then the for /r is the problem.
Since you are pushding to the appropriate directory, there is no need to specify the target directory in the for/r. It can be omitted or replaced by .
or perhaps echo %cd% directly after the pushd to show whether the pushd is the cause of the problem.
I realized that this would work for me (executed in cmd). However, it is plausible because I don't have any *.txt file in the main directory. Not the best solution but a workaround.
pushd "\\internal.company.com\path\"
rem first copy the desired files (text files) to the main-folder
for /f "tokens=*" %f in ('dir /a:-D /s /b myfile*') do copy "%f"
rem then make a new-folder and move them to there
md myfile
move *.txt "\\internal.company.com\path\myfile"
Note: if you want to execute this as a batch file, you need to use %%f instead of %f.
This will copy the files into main folder and then moves them into the desired sub-folder.
I have a folder which contains many sub-folders, each this sub-folder contains a program file named bk.bat in its root.
I want to write a bat file so that I can execute all these bk.bat files.
Please help.
Mine is folder structure
Create a batch file in your WWW folder and put this code in it.
FOR /F "delims=" %%G IN ('dir /a-d /b /s bk.bat') DO CALL "%%~G"
I've searched high and low for a solution to this problem but have so far drawn a blank. I'm fairly new to batch scripts, so apologies if this turns out to be a stupid question.
I have a list of sql filenames in a .txt file which I would like to merge into a single sql script. The .txt file and the .bat file are in one subdirectory of a root location, and the sql scripts are in a separate subdirectory like so:
root\
batch files\
.bat
.txt
views\
.sql
I can read the file names out the .txt file quite happily and pass them to a subroutine using this:
for /f %%d in (IncludeList.txt) do call :Include_List "%%d"
goto :eof
The part that is causing me problems is the :Include_List subroutine. I need to be able to search the views\ subdirectory for each specified filename and then copy the content of it into a new script.
I tried the following, which I think is the nearest I've got it to working, without it actually working:
:Include_List
for /r %%f in ('DIR /B /S ..\Views\vw*.sql ^| find /i %~1') do type %%f >> _All_views.sql
It seems to be treating each part of this ('DIR /B /S ..\Views\vw*.sql ^| find /i %~1') as a filename and then failing to find any of them. It's also checking inside the batch files\ directory and not the views\ directory.
One additional requirement, is that this is part of a larger batch file, which would need to continue processing after the files had been looped through and written to the newly created _all_views.sql file.
Is what I am trying to achieve even possible with a batch file? Or am I just going to have to manually add each required file into the batch file like this
type ..\Views\[filename].sql >> _All_views.sql
Based on wOxxOm's answer my final solution is as follows:
setlocal EnableDelayedExpansion
for /f %%d in (IncludeList.txt) do (
for /f "delims=" %%f in ('DIR /B /S ..\Views\vw*.sql ^| find /i "%%d"') do type %%f >> _All_views.sql
)
I did away with the subroutine as it was causing issues when placed inside a larger batch file, but a nested for loop works perfectly for what I need.
I have a folder structure like this:
C:\\\Logs\logs1\tracelogXXXX.log
C:\\\Logs\logs2\tracelogXXXX.log
C:\\\Logs\logs3\tracelogXXXX.log
Each folder has a bunch of tracelogXXXX's, and I've got the pseudocode for a script that loops through each folder, archives each log into its own .zip, and then delete the tracelog left outside the archive (because 7zip doesnt have move functionality).
But I have no batch experience really, and I can't even get the zipping to work properly.
I can't access the documentation for 7zip from where I am currently, so I've tried this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip"
And also this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip" "%%i\"
The first one goes and zips all of \Logs for each instance of a .log file, making many zips each bigger than the last. And the second makes zips for each instanceof a .log file, with nothing in them.
How do I just zip each log file, in its own zip, named after itself, while operating from the parent directory? Deleting the outer files afterwards doesn't seem hard to accomplish once I figure out whats wrong with this syntax, but this is the important part!
You can do this from the command line with no batch file needed:
FOR /F "usebackq tokens=* delims=" %A IN (`DIR "C:\Logs\*.log" /B /S`) DO "C:\Path\To\7za.exe" a "%~dpnA.zip" "%~fA" & DEL "%~fA"
To use in a batch file, just replace each % with %%.
I'm trying to automate a little tedious process I have to go through while updating files on my server. I have a content folder, with a lot of subfolders, each potentially containing some files. Some files have a compressed version (ending with .bz2). So a folder could have something like:
sound1.wav
sound1.wav.bz2
sound2.wav
texture1.tex
texture2.tex
texture2.tex.bz2
What I want to do is remove every file (somewhere in the content folder) that has an equivalent compressed file. Meaning in the above example I just want 'texture2.tex' and 'sound1.wav' removed.
for /r %%f in (*) do if exist "%%f.bz2" del "%%f"
Or, at the command line instead of in a batch file:
for /r %f in (*) do if exist "%f.bz2" del "%f"
Little mistake. It should be:
for /r %%f in (*.bz2) do if exist "%%f" del "%%f"
Or, at the command line instead of in a batch file:
for /r %f in (*.bz2) do if exist "%f" del "%f"