I'm wondering how I would exclude a file from being built within my batch file. Currently, my cl.exe compiliation line within the batch file looks like this:
cl /c ..\source\*.c
so how would I say I would like to build all files EXCEPT "file_not_needed.c" from the build?
The file(s) with the file extension c to exclude can be temporarily renamed with changing the file extension to exclude them.
A single command line for a single C source code file for usage in a command prompt window or a batch file:
ren ..\source\file_not_needed.c file_not_needed.c.tmp & cl.exe /c ..\source\*.c & ren ..\source\file_not_needed.c.tmp file_not_needed.c
A multi-line solution for multiple C source code files for usage in a batch file:
for %%I in (file_not_needed "one more file not needed") do ren "..\source\%%~I.c" "%%~I.c.tmp"
cl.exe /c ..\source\*.c
for %%I in ("..\source\*.c.tmp") do ren "%%I" "%%~nI"
All the files to exclude must be specified inside the round brackets of first FOR loop using a space, comma or semicolon as separator between the file names without file extension.
Related
Here is an example for one file:
C:\folder\video\my-holiday-video.mp4
C:\folder\image\my-holiday-screen.png
C:\folder\mix\my-holiday-HDphoto.jpg
C:\folder\cover\my-holiday-art.jpg
What I try to do is to make a ZIP archive of files which are in different folders, but have the same name, i.e. my-holiday for the example above, but a different suffix as -video or -screen or -HDphoto or -art and keep the same folder tree in each ZIP file.
The result should be my-holiday.zip which contains all the files in their folders.
I have success with this code on all the files have the same name:
#for %%I in ("%~dp0wheel\*") do #"%ProgramFiles%\7-Zip\7z.exe" a -bd -bso0 -mx0 -r -x!*.zip -y "%~dp0%%~nI.zip" "%~dp0%%~nI.*"
But it zips only when files have all the strict same file name.
Is there a way to make my batch ignore the suffixes -video, -screen, -HDphoto and -art in the name of each file in order to make them zip together?
Here are the requirements:
The suffixes -video, -screen, -HDphoto and -art are always the same for all my files. So these are fixed in the file names.
There should be in the file my-holiday.zip:
video\my-holiday-video.mp4
image\my-holiday-screen.png
mix\my-holiday-HDphoto.jpg
cover\my-holiday-art.jpg
Each file is with its relative path in the ZIP archive. So when I decompress them, they'll be extracted in their directories, for example my-holiday-video.mp4 will decompress into folder video, my-holiday-HDphoto.jpg into folder mix, etc. like my actual code do it.
The rule for common part of a files collection is that everything left to -video before file extension .mp4 should be used as common part of the file names to pack into the ZIP file.
The folder video is the source for packing the files with common name into a ZIP file.
The task could be done with following batch file:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
cd /D "C:\folder" || exit /B
for %%I in ("video\*-video.*") do (
set "VideoFileName=%%~nxI"
setlocal EnableDelayedExpansion
set "CommonName=!VideoFileName:-video%%~xI=!"
"%ProgramFiles%\7-Zip\7z.exe" a -bd -bso0 -mx0 -y -- "!CommonName!.zip" "cover\!CommonName!-art.*" "image\!CommonName!-screen.*" "mix\!CommonName!-HDphoto.*" "video\!VideoFileName!"
endlocal
)
endlocal
The batch file defines with the first two command lines the required execution environment which is:
command echo mode turned off
command extensions enabled
delayed expansion disabled
The next command changes the current directory to C:\folder. If that command fails because of the directory does not exist or a UNC path is used instead of a path starting with a drive letter and a colon, the batch file processing is exited without any further message than the error message output by command CD.
The FOR loop searches in subdirectory video for non-hidden files matching the wildcard pattern *-video.*. The file name with file extension of a found video file matching this wildcard pattern is assigned to the environment variable VideoFileName.
Next delayed expansion is enabled as required to make use of the file name assigned to the environment variable VideoFileName. Please read this answer for details on what happens in background on execution of setlocal EnableDelayedExpansion and later on execution of corresponding endlocal.
A case-insensitive string substitution using delayed expansion is used to remove from video file name -video left to the file extension and the file extension itself which can be .mp4, .mpg, .mpeg, etc. VideoFileName was defined with file extension in case of the file name itself contains anywhere the string -video which should not be removed by the string substitution. For example My Home-Video-video.mp4 assigned to VideoFileName results in My Home-Video getting assigned to the environment variable CommonName because of taking also the file extension into account on string substitution.
Next 7-Zip is executed with the command a and the switches as posted in question to create or add to a ZIP file in current directory with common name part of the files in the four directories and file extension .zip the video file name and the other image files from the other three directories with whatever file extension the images have in the other three directories.
Then endlocal is executed to restore the previous environment with delayed expansion disabled again.
The commands setlocal EnableDelayedExpansion and endlocal are used inside the FOR loop to be able to process correct also a files collection with a common name like Superman & Batman (+ Robin!) containing an exclamation mark.
The video files in subdirectory video define which files to pack into a ZIP file. So all image files in the three image directories are ignored for which no video file exists in directory video.
Note: The batch file is not capable processing correct video file names which contain an exclamation mark in the file extension. I doubt that this limitation is ever a problem as I have never seen a video file extension with an exclamation mark.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
echo /?
endlocal /?
exit /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file for an explanation of conditional operator ||.
This question already has answers here:
How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?
(6 answers)
Closed 9 months ago.
I need to write a batch script that will allow me to unzip a .zip file into a new folder that has the same name as the zip file. I cannot assume that I have some program to unzip the file; it has to work as if the computer is brand new.
For example, if I have a group of zip files named a1, b2, c3, and d4, I want their contents to go into 4 different folders labeled a1, b2, c3, d4. It doesn't need to be concise, it just has to work.
Read excellent topic How can I compress (zip) and uncompress (unzip) files and folders with batch file without using any external tools? written by npocmaka.
I suggest to use the second solution: Using Shell.Application
The task can be done with zipjs.bat also written by npocmaka with a batch file with just a few lines:
#echo off
for %%I in (*.zip) do call "%~dp0zipjs.bat" unzip -source "%%~fI" -destination "%%~dpnI\" -force yes
for /F "delims=" %%D in ('dir /AD /B "%TEMP%\*.zip" 2^>nul') do (
%SystemRoot%\System32\attrib.exe -h "%TEMP%\%%~D"
rd /S /Q "%TEMP%\%%~D" 2>nul
)
The hybrid batch file zipjs.bat must be in same folder as the batch file with the two lines above.
Running in a command prompt window call /? explains %~dp0 which means drive and path of argument 0 of batch file without surrounding quotes ending always with a backslash. Argument 0 of a called batch file is the name of the batch file, see also What does %~dp0 mean, and how does it work?
Help of command for displayed on running in a command prompt window for /? explains
%%~fI - full file/folder name without surrounding quotes, and
%%~dpnI - drive, path and name of a file/folder without surrounding quotes and without extension
for loop variables like call /? does for batch parameters/arguments.
The second for loop finds all subdirectories in directory for temporary files with .zip in directory name, removes the hidden attribute and deletes those subdirectories. This is necessary as zipjs.bat first extracts each file into a newly created subdirectory in directory for temporary files and leaves them there after successful extraction.
I wish to delete a line contaning a specific word (in my case it is [*KEYWORD]). Those files are having an extension '.blk'. These files are located in different subfolders in the same folder. I have written this followng code.
#setlocal enabledelayedexpansion
#echo off
dir "C:\Users\XFIWSM\Desktop\batch_files\T30\*.blk /s/b > dirlist.txt
for /F %%i in (dirlist.txt) do (
type %%i
findstr /v "KEYWORD"
> %%i.blk
)
pause
As per ansswer provided by Mr. Ken, i have tried with above code. But, nothing seems to work. i have created an empty file named dirlist.txt . But this file is also not being written with path of *.blk files. Also i wish to save the .blk file with same name at the same location. Therefore, i am trying to use > %%i.blk ... Can anyone please help me, where am i going wrong ?
You can't use type on files that aren't in the specified directory the way you are (your files are in subfolders of the specified directory, not in the directory itself).
My suggestion would be to use a dir command, with the appropriate switches to search subdirectories (/s) and to produce a bare listing (without sizes, dates, and folder/volume names) (/b), and redirect that to a text file. You can then use a for /f loop to read each line in that directory listing and process it further.
#setlocal enabledelayedexpansion
#echo off
dir "D:\Master\SPRINGBACK\DPK\T08\*.blk /s/b > dirlist.txt
for /F %%i in (dirlist.txt) do (
:: Process each file here
)
For information about removing a specific line of text from a file, you can see How to remove lines (or text in given lines) from file in batch?
if you have a machine with grep, (checkout unxutils on sourceforge if using windows), you could use this commmand:
grep "KEYWORD" -v *.blk > filewithlineremoved.blk
I'm trying to use the FOR /f command to calculate the filesize of multiple files with the same extension. I have this so far:
FOR /F "usebackq" %%A IN ('C:\Users\%username%\*.bat') DO set size=%%~zA
If I have 3 batch files under the desktop, and 2 under my documents, I want the batch file to get the size of each and display all 5 together. I have the command working perfectly if I use just a single directory with batch files, but all I need is for it to include subdirectories. The current line I have above displays ECHO is off because there are NO batch files under the path C:\Users\%username%. I have batch files on my desktop, and I need it to include it since it's a subdirectory. How can I get this command to include subdirectories?
FOR /R
Loop through files (Recurse subfolders)
Syntax
FOR /R [[drive:]path] %%parameter IN (set) DO command
Key
drive:path : The folder tree where the files are located.
set : A set of one or more files. Wildcards must be used.
If (set) is a period character (.) then FOR will
loop through every folder.
command : The command(s) to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)
This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.
If the [drive:]path are not specified they will default to the current drive:path.
Unlike some other variants of the FOR command you must include a wildcard (either * or ?) in the 'set' to get consistent results returned. In many cases you can work around this by adding a single character wildcard e.g. if you are looping through multiple folders to find the exact filename myfile.txt you could instead specify myfile.t?t
http://ss64.com/nt/for_r.html
dir is far easier.
dir "%userprofile%\*.bat" /a /s
or just the sizes
dir "%userprofile%\*.bat" /a /s|findstr /c:"File(s)"
I need to convert some xls files into xlsx files. I can successfully convert one xls file into xlsx by running this command into cmd prompt (windows):
ssconvert inputFileName.xls outputFileName.xlsx
(ssconvert is a Gnumeric's command-line utility that can convert between different spreadsheet file formats)
I'd like to write a batch file that FOR EACH file in a specified directory runs the command I wrote above, using the current file name both for input and for output filename.
For example, if I have this set of files:
c:\directory\file1.xls
c:\directory\file2.xls
c:\directory\file3.xls
the output should be
c:\directory\file1.xlsx
c:\directory\file2.xlsx
c:\directory\file3.xlsx
so the batch pseudo code should be something like
directory = c:\directory\
for (fileName in directory)
ssconvert fileName.xls fileName.xlsx
Can anyone help me?
for /r %%v in (*.xls) do ssconvert "%%v" "%%vx"
a couple have people have asked me to explain this, so:
Part 1: for /r %%v in (*.xls)
This part returns an array of files in the current directory that have the xls extension. The %% may look a little curious. This is basically the special % character from command line as used in %PATH% or %TEMP%. To use it in a batch file we need to escape it like so: %%PATH%% or %%TEMP%%. In this case we are simply escaping the temporary variable v, which will hold our array of filenames.
We are using the /r switch to search for files recursively, so any matching files in child folders will also be located.
Part 2: do ssconvert "%%v" "%%vx"
This second part is what will get executed once per matching filename, so if the following files were present in the current folder:
c:\temp\mySheet.xls,
c:\temp\mySheet_yesterday.xls,
c:\temp\mySheet_20160902.xls
the following commands would be executed:
ssconvert "c:\temp\mySheet.xls" "c:\temp\mySheet.xlsx"
ssconvert "c:\temp\mySheet_yesterday.xls" "c:\temp\mySheet_yesterday.xlsx"
ssconvert "c:\temp\mySheet_20160902.xls" "c:\temp\mySheet_20160902.xlsx"
Actually this is pretty easy since Windows Vista. Microsoft added the command FORFILES
in your case
forfiles /p c:\directory /m *.xls /c "cmd /c ssconvert #file #fname.xlsx"
the only weird thing with this command is that forfiles automatically adds double quotes around #file and #fname. but it should work anyway
you can run something like this (paste the code bellow in a .bat, or if you want it to run interractively replace the %% by % :
for %%i in (c:\directory\*.xls) do ssconvert %%i %%i.xlsx
If you can run powershell it will be :
Get-ChildItem -Path c:\directory -filter *.xls | foreach {ssconvert $($_.FullName) $($_.baseName).xlsx }
I am doing similar thing to compile all the c files in a directory.
for iterating files in different directory try this.
set codedirectory=C:\Users\code
for /r %codedirectory% %%i in (*.c) do
( some GCC commands )