Using Forfiles in order to delete logs older than 1 day, but the command fails because one of the logs is being used - windows-server-2008-r2

I went through the forfiles /? but couldn't find a way to skip these files.
My command looks like this:
forfiles /P e:logfiles\ /S /M *.log /D -1 /C "cmd /c del /f #fname.log"
Is there a way to do it?
Thanks!

Related

Batch file : how to rename a .xls file when another .xlsx file present in same folder?

I have a folder with 1 batch file and 2 excel files - old.xls and new.xlsx.
I need to rename both these files to test.xls and test.xlsx by using the batch file in the same folder.
When I try to rename the .xlsx file I can successfully do it by using the following command:
ren *.xlsx test.xlsx
But when i try doing the same for the .xls file I get the following error :
ren *.xls test.xls
A duplicate file name exists, or the file
cannot be found.
Can someone tell me how to do this? I am new to batch files.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
ECHO directory before xlsx rename
DIR /x /a-d
REN *.xlsx test.xlsx
ECHO ============================
ECHO directory before XLS rename
DIR /x /a-d
ECHO.
FOR %%a IN (*.xls) DO ECHO "XLS" name found: %%a
ECHO.
FOR %%a IN (*.xls) DO IF /i "%%~xa"==".xls" REN "%%a" test.xls
ECHO ============================
ECHO directory after XLS rename
DIR /x /a-d
popd
GOTO :EOF
By default, every file or directory whose name does not comply with the DOS "8.3" filename convention (name of up to 8 characters, extension of up to 3, separated by a dot) will be assigned a "short name" that does comply.
The above code (I use u:\sourcedir as a test directory) will show the steps. Note that dir /x shows shortname and longname. /a-d suppresses directorynames).
so - the magic is to check that the fullname-extension actually is .xls.

Equivalent for find -o (shell) in .cmd

Can someone tell me the .cmd equivalent command for the shell command find -o -samefile ?
I am trying to loop inside folders to copy files into a different location.
My original shell script is :
for qapi_file in find . -type d -iname qapi_export -o -samefile qapi/common -o -samefile core/api; do cp -p $qapi_file/qapi*.h include/qapi/
I have tried doing this:
FOR /d %%i IN ( DIR -i qapi_export qapi\common\ core\api) do (xcopy /O %i%\qapi*.h include\qapi)
this does not work as I expect it to. If I use for /d , it loops once and returns no files to copy. On the other hand if I use for /r the for loop becomes endless and I am unable to break the loop (I tried using goto :eof for this but it gives an error - goto was unexpected here)
I am pretty new to this and am unsure how to proceed further. I would appreciate any input in this regard
Using %%i instead of %i% and looping through all subdirectories with /r will do the trick:
for /r . %%i in (.) do (
xcopy /y/o %%i\qapi_export\qapi*.h include\qapi
xcopy /y/o %%i\qapi\common\qapi*.h include\qapi
xcopy /y/o %%i\core\api\qapi*.h include\qapi
) >nul

Search and Copy through batch file

I would like to search all word files in .doc and .docx extensions from C and D drives then copy then to F drive the script which i want have to use xcopy command any suggestions
*.doc will match *.doc and *.docx
xcopy "c:\*.doc" "f:\target folder\c\" /s
xcopy "d:\*.doc" "f:\target folder\d\" /s

Batch Files: list folders that contain a folder named region as a sub-folder

I need batch code that will #echo the folder names, ONLY if they include a folder inside them that is called SPECIFICALLY "region".
Thanks in advance.
This should do it:
#echo off
for /f %%a in ('dir /s/b/ad') do (
if /i "%%~na" EQU "region" echo %%~dpa
)
#echo off
for /r "c:\somewhere\toStart" %%f in (.) do (
if /i "%%~nxf"=="region" echo "%%~dpf"
)
For each folder in the structure begining at c:\somewhere\toStart, if name and extension of folder are "region", then echo the drive and path to the folder (the folder containing the region folder).

Bash to Batch conversion

After making a bash file that creates directories and then transfers files from one directory to each created directories I am curious as to how I'd go about doing this in a batch file.
Here's the bash code:
#!/bin/bash
# For each item in file named in $1, make a directory with this name.
# and copy all files named in file $2 from templates folder to new directory
for user in `cat $1`
do
if [ -d $user ]
then
echo Directory $user already exists
rm -r $user
echo $user has been deleted
fi
mkdir $user
echo Directory $user created
for file in `cat $2`
do
cp /home/student/Desktop/OS/templates/$file $user
chmod 700 $user/$file
done
echo Directory for $user set up
done
Any input would be greatly appreciated
Although I don't know Bash, I think my Batch version of your program is precise:
#echo off
rem For each line in file named in %1, make a directory with this name.
rem and copy all files named in file %2 from templates folder to new directory
for /F "delims=" %%u in (%1) do (
if exist "%%u" (
echo Directory %%u already exists
rd /S /Q "%%u"
echo %%u has been deleted
)
md "%%u"
echo Directory %%u created
for /F "delims=" %%f in (%2) do (
copy "/home/student/Desktop/OS/templates/%%f" "%%u"
rem chmod not exist in Batch
)
)
However, I would modify previous program in order to read the templates file just once and store its lines in an array:
#echo off
rem For each line in file named in %1, make a directory with this name.
rem and copy all files named in file %2 from templates folder to new directory
setlocal EnableDelayedExpansion
set temp=0
for /F "delims=" %%f in (%2) do (
set /A temp+=1
set template[!temp!]=%%f
)
for /F "delims=" %%u in (%1) do (
if exist "%%u" (
echo Directory %%u already exists
rd /S /Q "%%u"
echo %%u has been deleted
)
md "%%u"
echo Directory %%u created
for /L %%i in (1,1,%temp%) do (
copy "/home/student/Desktop/OS/templates/!template[%%i]!" "%%u"
rem chmod not exist in Batch
)
)
Antonio aka "Aacini"

Resources