I'm trying to find a way to copy two *.log files into one file. My intent was to use For /R to set each filename to a variable. Not as easy as I thought. The log files will have different random names each week that I want to combine into one file called Kip.log.
I only got as far as:
FOR /R D:\Temp %F in (*.log) do echo %~nxF
I don't know how to get further.
Assuming the two log files are in d:\temp then this will suffice.
#echo off
pushd "d:\temp"
copy *.log kip.txt
ren kip.txt kip.log
If you can specify just the files you want using wildcards then this will work
copy *.log kip.log
or
copy a*.log+b*.log kip.log
Type copy /? for Help
To set the variables (and then append) do this:
pushd D:\Test
dir /b *.log >> files.tmp
setlocal enabledelayedexpansion
set count=0
for /f %%a in (files.tmp) do (
set /a count+=1
set "file!1!=%%~a"
)
:: Now you have 2 variables (or more) all file[number]
:: Below is to append
copy "%file1%"+"%file2%" Kip.log
Hope that helped.
#ECHO OFF
SETLOCAL
(
FOR /r D:\temp %%F IN (
zd*.*
) DO TYPE "%%~fF"&echo========%%~fF============
)>KIP.LOG
GOTO :EOF
You don't say why you need to set two variables; for the concatenation task, it's not necessary.
the &echo========%%~fF============ above simply adds a separator with the filename found. It's not required and can be omitted if you wish.
If you "need" the filenames in variables simply to delete them, then
del /s d:\temp*.log
would make short work of that task.
Related
I've been attempting to remove a certain string from a bunch of files but I am unable to do it. The part I want to rename is the .english in my files. How would I do this?
https://i.stack.imgur.com/rhRKG.png
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following setting for the source directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
FOR /f "delims=" %%b IN (
'dir /b /a-d "%sourcedir%\*.english.*" '
) DO (
SET "newname=%%b"
ECHO REN "%sourcedir%\%%b" "!newname:.english.=.!"
)
GOTO :EOF
Always verify against a test directory before applying to real data.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Because your string splitting is at periods, ., I'd offer the following methodology.
In cmd.exe:
for %g in ("*.english.wav") do #for %h in ("%~ng") do #ren "%~g" "%~nh%~xg"
From a batch file:
#For %%G In ("*.english.wav") Do #For %%H In ("%%~nG") Do #Ren "%%~G" "%%~nH%%~xG"
As your question is unclear as to the intended final filename.
If you wanted to replace .english with something else, lets say .german, rather than simply removing it, then change:
"%~nH%~xG", or "%%~nH%%~xG"
To:
"%~nH.german%~xG", or "%%~nH.german%%~xG"
In advance: I'm new to batch or programming in general so please explain as much as possible.
My problem:
I want to delete all folders not named X and Y in a directory Z (ex. D:\Test\z)
So let's say Z contains these folder:
backup
resources
project1
project2
project3
I'd need to exclude only backup and resources from deletion via a script.
I looked up multiple soultions for not deleting only one folder with a certain name but I don't know if it's possible for multiple values with Batch.
not tested:
#echo off
set "root_dir=C:\Z"
set "exclude_list=backup resources"
pushd "%root_dir%"
for /f "tokens=* delims=" %%# in ('dir /b /a:d^| findstr /v "%exclude_list%"') do (
echo rd /s /q "%%~f#"
)
if this looks ok delete the echo on the last line to activate deletion.
If all folders you want to delete are like project* you can try also :
for /d %%a in ("project*") do rd /s /q "%%~fa"
#echo off
setlocal EnableDelayedExpansion
rem Define working variables
set "dir=D:\Test\z"
set "exclude=/backup/resources/"
rem Change current dir to working dir
cd /D "%dir%"
rem Process all folders in this dir
for /D %%f in (*) do (
rem If current folder is not in "exclude" var
if "!exclude:/%%f/=!" equ "%exclude%" (
rem Delete it
ECHO rd /s /q "%%f"
)
)
This method use internal cmd.exe commands only, so it run faster than other methods that may use external .exe files (like findstr.exe).
The way to detect if a name is in the exclude variable is trying to delete such name from it: "%exclude:/%%f/=%": if the result is equal to the original variable contents, the folder was not there. This method is very simple and efficient and works not matter the case of the letters, so it don't requires any /I ignore case switch in the if command.
The names are delimited by slashes to avoid any problem caused by partial name matches; for this reason the %%f part is enclosed in slashes in the if command.
Note that the value of %%f change in each iteration of the for command. For this reason, the exclude variable is surrounded by exclamation marks instead percent signs and the setlocal EnableDelayedExpansion command is given at beginning; otherwise, the %expansion% would be done just one time, before the for command start iterations. You may look for "delayed expansion" in this forum for a further explanation of this point.
I want to rename the all PDF files in folder using batch script. for example i have 3 files in folder:-
anyfile.pdf
otherfile.pdf,
another.pdf
Now i want to rename file as bellow:-
PDF0.pdf
PDF1.pdf,
PDF2.pdf
i have fetch the files using this script:-
#ECHO OFF
SETLOCAL DisableDelayedExpansion
SET "r=%__CD__%"
FOR /R . %%F IN (*.pdf) DO (
SET "p=%%F"
SETLOCAL EnableDelayedExpansion
ECHO(!p:%r%=!
ENDLOCAL
)
pause
now i can rename please help me.
Thanks
Are you just looking for the command to rename files? Its ren. Look at http://ss64.com/nt/ren.html for more info.
FOR /R and the string replacement to get rid of the path seem unnecessary here, since you stay within one directory.
(generally, if you want to get of the path, just say %%~nxFwhich returns the Name and eXtension of %%F.)
you can perform arithmetics, ie. count a number up, with SET /A, so you could do simply
#ECHO OFF
setlocal enabledelayedexpansion
set i=0
FOR %%F IN (*.pdf) DO (
set /a i=i+1
ren %%F PDF!i!.pdf
)
pause
I have a bat file to process all file under a directory, and output to another directory
this is the codes in the bat file
#set dir1=mulit-simp\
#set dir2=mulit-trad\
#cd /d %~dp0
#if not exist %dir1% md %dir1%
#if not exist %dir2% md %dir2%
#for /f "delims=" %%i in ('dir %dir1%*.* /b') do #opencc.exe --input="%dir1%%%i" --output="%dir2%%%i"
#echo.
#echo Done!
#echo.
#pause
but this code can't process files in the sub directory
How could I process the sub directory files, and output them with the same directory structure to another directory?
thx for help :)
The DIR /S option would give you the entire folder hierarchy. And you also want the /A-D option to suppress folders from the output. Type HELP DIR or DIR /? for more info.
However, instead of using FOR /F with DIR, I recommend using FOR /R. Type HELP FOR or FOR /? for more info. Also see http://judago.webs.com/batchforloops.htm for easier to understand explanation of the FOR command.
Either way, you also need to create all of the destination folders. The easiest way to do that is to use XCOPY /T /E to create the destination folders prior to processing the files.
Some additional tips:
Simply put #echo off at the top instead of prefixing every command with #
ECHO. has been widely used for years, but it can actually fail to work properly. Safer to use ECHO( instead.
You probably don't want the environment variable you define to hang around after the batch is finished. Putting SETLOCAL at the top will make the variables temporary and they will exist only until the batch file finishes, or until ENDLOCAL is executed. Given that you are using PAUSE at the end, I suspect you are running the batch file by double clicking on the file or a shortcut. If so, then the command window closes at the end, and the variables disappear along with it. Then SETLOCAL is not needed, but it is still probably a good practice to get into anyway.
Here is the finished code with all of the suggestions. EDIT - The original code did not work. Altered to address tiance's comments
#echo off
setlocal
set dir1=mulit-simp
set dir2=mulit-trad
cd "%~dp0"
:: Replicate tree
if not exist "%dir1%" md "%dir1%"
if not exist "%dir2%" md "%dir2%"
xcopy /t /e "%dir1%" "%dir2%"
:: Get length of absolute path of %dir1%
<nul set /p "=%cd%\%dir1%" >getLength.tmp
for %%F in (getLength.tmp) do set len=%%~zF
del getLength.tmp
:: Process the files
for /r "%dir1%" %%F in (*) do (
set "src=%%F"
setlocal enableDelayedExpansion
opencc.exe --input="!src!" --output="%dir2%!src:~%len%!"
endlocal
)
echo(
echo Done!
echo(
pause
Have you tried XCOPY sourcefolder destinationfolder /E ?
Using this batch file I want zip some *.txt files. Each *.txt file in its own zip file. Unfortunately it is not working and i get as output
ECHO is disabled (OFF).
several times. Here is the sourcecode:
#echo off
setlocal EnableDelayedExpansion
for %%i in (*.txt) do (
set filename = %%i
set filenametrunc = %filename:~0,10%
7z a -tzip -mx0 %zipname%
echo %filename% zipped.
)
I read something about EnableDelayedExpansion and activated it. Can't get it working though.
Any help appreciated.
You need to use SetLocal EnableDelayedExpansion and wrap variables in !. Also, don't put spaces between variable names, equals and the value.
set filename = ... makes a variable named %filename % with a value ...
set filename=..... makes a variable named %filename% with a value .....
#echo off
setlocal EnableDelayedExpansion
for %%i in (*.txt) do (
set filename=%%i
set filenametrunc=!filename:~0,10!
7z a -tzip -mx0 !zipname!
echo !filename! zipped.
)
Variables in % inside brackets are evaluated all at the same time, which is before the entire loop starts executing. Your previous code would expand all variables before the set statement ran.
You need to initialize the variable zipname and expand the var using the ! character instead of the % character.
Read HELP SET, specifically
Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.
and change your code to
#echo off
setlocal EnableDelayedExpansion
for %%i in (*.txt) do (
set zipname=%%i
7z a -tzip -mx0 !zipname!
echo !zipname! zipped.
)
Delayed expansion is only needed within the loop if you need to access a variable that you assign inside the loop. But there is no need in your case.
You need to 1st get the correct 7z syntax. Your original code was attempting to put all of the the files in the current directory into a single zip file because you didn't specify a filename. Also your ZIPNAME was uninitialized. You want something like the following.
7z a -tzip zipname filename
I presume you want the name of the zip to be the same as the original filename, except with a .zip prefix instead of .txt. Then all you need is the ~n modifier that gives the base name without the prefix. 7-Zip will automatically append the .zip extension.
for %%i in (*.txt) do 7z a -tzip %%~ni %%i
If you want you can add the -mx0 option, which does no compression. I can't imagine why you would do that for a text file.
for %%i in (*.txt) do 7z a -mx0 -tzip %%~ni %%i
If you want to add your own message
for %%i in (*.txt) do (
7z a -mx0 -tzip %%~ni %%i
echo %%i zipped into %%~ni.zip
)