Search and Copy through batch file - search

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

Related

Delete line from all the files in a folder with string 'Generation'

I want to delete a line with string 'Generation' in all the files under a folder named KCG. I tried the following in Windows OS command prompt:
sed -i '/Generation/d' file
But got the following error message:
sed: can't read file: No such file or directory
Next I tried:
sed -i '/Generation/d' airport_related_altitudes_derived_data.c
This worked, but I do not want to enter the filenames of all the files in the folder each time. Is there a command to recursively look for the string in all the files under the folder?
Try this:
find . -type f -exec sed -i '/Generation/d' {} \;
That will recursively find all files ("-type f") under the current directory (".") and call "sed -i '/Generation/d'" on each file ("{}").
A windows way to enumerate files of a pattern/file type in current folder.
#Echo off
For %%A in (*.c) Do sed -i '/Generation/d' %%A
Recursive from a start path
For /r "X:\start\path" %%A in (*.c) do sed -i '/Generation/d' %%A
A pure batch way to drop lines containing words from a file is using find /v or findstr /v but requires a different output file name, no inplace editing.
#Echo off
for %%A in (*.c) do findstr /vi "Generation" "%%~A" >"%%~dpnA_new%%~xA"
what creates new files with a appended _new before the extension.
It's of course possible to rename the old to .bak and write the changed version to the original name.

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

Convert all .docx to .odt inside folder system and then delete all .docx files

i have a folder system which has 2 level deep subfolders. Each folder has some .docx files and also other files.
What i need to do is that, i want to convert all these .docx files into .odt files and after that i want to delete these old .docx files to remove duplication.
i got bash command Linux Command to convert .docx to .odt , but for this command to run i have to go inside the folder in which .docx files exists through terminal.
so i am looking for 1 linux command or shell file script which does this automatically.
Thanks in advance.
If you're using zsh or bash version 5.0 or higher, this is trivial:
#!/bin/zsh
cd $1 && libreoffice --headless --convert-to odt **/*.docx
With any sh-like shell, you can also do it using the following pipe:
#!/bin/sh
find $1 -name '*.docx' -print | xargs libreoffice --headless --convert-to odt
cd $1 && find $1 -name '*.docx' -delete

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

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!

Resources