I am trying to get all the newest file from subdirectories into one network directory with a command without getting the subdirectory structures. They are the SQL Server log files with extension of *.trn. I have the following but it doesn't work.
Trying to get only the newest *.trn files from ...........Backup and it's subdirectories.
for /R E:\SQLSERVER\PRODINSTANCE1\Backup %%f in (*.trn) do xcopy %%f "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
You can use the dir command with the /od switch and a for loop:
#echo off &setlocal enabledelayedexpansion
for /d /r "E:\SQLSERVER\PRODINSTANCE1\Backup" %%a in (*) do (
for /f "delims=" %%i in ('dir /b /a-d /od "%%~a"') do set "newest=%%~fi"
xcopy "!newest!" "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
)
For more help enter help dir in the command prompt.
I'm not sure if you're using xcopy correctly. /O doesn't take arguments. It only copies ownership/ACL info. (Is that really what you want for log files?)
Since you didn't describe what "doesn't work" means, my only suggestion is to hedge against file names with spaces in them.
FOR /R "%SRC_DIR%" %%f in (*.trn) do xcopy "%%~f" "%DEST_DIR%" /B /O /D /Y
This worked for me (I tested with .pdf's).
Related
I am trying to rename a group of files in a directory that all have a part in the filename I want removed later on.
The example of names are:
123876.111thepartIwanttoremove.exe
thisisatestfile.111thepartIwanttoremove.exe
this?392!.111thepartIwanttoremove.exe
thankyouall.222thepartIwanttoremove.exe
test.222thepartIwanttoremove.exe
whatis#d354.222thepartIwanttoremove.exe
My code is:
forfiles /S /M *.111thepartIwanttoremove.exe /C "cmd /c rename #file #fname.doc"
ren ???.111thepartIwanttoremove.* ???.doc
ren ????.111thepartIwanttoremove.* ????.doc
ren ?????.111thepartIwanttoremove.* ?????.doc (and so on)
forfiles /S /M *.222thepartIwanttoremove.exe /C "cmd /c rename #file #fname.jpg"
ren ???.222thepartIwanttoremove.* ???.jpg
ren ????.222thepartIwanttoremove.* ????.jpg
ren ?????.222thepartIwanttoremove.* ?????.jpg (and so on)
So for an example I want the file:
123876.111thepartIwanttoremove.exe
To look like this:
123876.doc
what function can I use to remove the .*thepartIwanttoremove.exe part of the name afterwards without writing so many lines with "?"?
Thank you very much for your help.
Assuming all the files have three parts to the file name that are separated by periods, this style of code should work for you.
for /F "tokens=1-3 delims=." %%G in ('dir /a-d /b *.111*.exe') do rename "%%G.%%H.%%I" "%%G.doc"
for /F "tokens=1-3 delims=." %%G in ('dir /a-d /b *.222*.exe') do rename "%%G.%%H.%%I" "%%G.jpg"
I have a bat file which part of looks like:
set test=test
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\path\%test%\%%x\ /S /E /F
xcopy ..\dir c:\path\%test%\dir\ /S /E /F
The for loop does not work, but the xcopy does. If I move the contents of the directory above to the current directory and change the code to remove the "..\":
for /d %%x in (*.%test%) do xcopy "%%x" c:\path\%test%\%%x\ /S /E /F
it works. Can someone please tell me why the bat script in the for loop cannot look up a directory? Am I approaching this wrong?
EDIT: I have now changed the command to after seeing ths answer but it still does not work:
for /d %%~nxx in (..\*.%MUI%) do xcopy "%%~nxx" c:\temp\%test%\%%~nxx\ /S /E /F
I receive the error:
%~nxx was unexpected at this time
EDIT #2:
I still cannot get it to work, my commands have looked like
for /d %%x in (..\*.%test%) do xcopy "%%~nxx" c:\temp\%test%\%%~nxx\ /S /E /F
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\temp\%test%\%%~nxx\ /S /E /F
It can. But %%x will contain ..\xyz.test, not xyz.test, which is probably not what you want in your xcopy target.
Replace it with %%~nxx (for "Name and eXtension of x") to chop of the path.
for /d %%x in (..\*.%test%) do xcopy "%%x" c:\path\%test%\%%~nxx\ /S /E /F
I have a folder, which has files and folders inside it like
C:/MyFolder
C:/MyFolder/File1.txt
C:/MyFolder/File2.txt
C:/MyFolder/File3.sql
C:/MyFolder/Folder1
C:/MyFolder/Folder1/File5.txt
What batch command do I need to use to delete all the folders and contents inside them without deleting files inside my folder. Example : Delete Folder1,Folder1/File5.txt but retain File1.txt,File2.txt and File3.sql?
This shows you the commands - if you are happy with them then remove the echo keyword and run it again.
#echo off
for /d %%a in ("C:\MyFolder\*") do echo rd "%%a" /q /s
pause
from command prompt:
for /f "tokens=* delims=" %a in ('dir /b /a:d "C:\someDir"') do #rd /s /q "%~fa"
from batch file:
for /f "tokens=* delims=" %%a in ('dir /b /a:d "C:\someDir"') do #rd /s /q "%%~fa"
For DOS/Command prompt use
for /d %F in ("path*") do rmdir /s /q "%F"
Use double % if you use it in a batch file.
for /d %%F in ("H:\EDGE-backup*") do rmdir /s /q "%%F"
I used this to backup the EDGE bookmarks and such, and since XCOPY always brings with it it's root directory subfolders i had to delete these after the copy.
The above worked for this. Result,only files remained in H:\EDGE-backup.
I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)
Here is my folder hierarchy:
[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]
[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]
...
[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]
I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do?
I tried the below code and it's not work because it coppies under the all subfolders.
for /r %NUMBER_DIRS% %%d in (.) do (
copy %PROJECT_INPUTS%\*.txt "%%d"
)
Thanks.
Try this:
for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"
This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.
Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.
I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.