How can I modify this batch script's directory? - batch-file

This batch script creates a text file of a list of files in its own directory, sans file extensions. However, I would like it to create a list of another directory's files instead. How could I modify it to attain those results?
#echo off
cd %1
if exist filelisting.txt del filelisting.txt
for /F "delims=" %%j in ('dir /A-D /B /O:GEN') do echo %%~nj >> filelisting.txt

Nevermind, I figured it out:
#echo off
cd %1
if exist filelisting.txt del filelisting.txt
for /F "delims=" %%j in ('dir "c:\Location\*" /A-D /B /O:GEN') do echo %%~nj >> filelisting.txt

Related

Batch file - copy two last modified files

I would like to write a .bat file to move the two last modified files of a specific extension *.bak in directory a to a different directory.
I used this line to copy files:
robocopy D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups *.bak /S
I'm new with this and have no idea how to tweak this to get the result I need.
Thanks
not tested:
#echo off
for /f "tokens=* delims=" %%# in (' dir /a:-d /o:-d /t:a /b "D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups\*.bak"') do (
if not defined last set "pre_last=%%~f#"
set "last=%%~f#"
)
copy /y "%last%" "c:\new_dir"
copy /y "%pre_last%" "c:\new_dir"
#echo off
setlocal EnableDelayedExpansion
cd "D:\DailyBackup\IDMRObjects\SQLBackups"
set copied=0
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W *.bak') do (
copy "%%a" "other\dir"
set /A copied+=1
if !copied! equ 2 goto break
)
:break

Create BAT file to get directory listing up to 2nd level only

I have a window directory with 300 folders inside of it and 4 levels of folders within each folder. I need a listing of folders only up to 2nd level in a txt file? I need the full path of each directory as well. I would like to use this a BAT file or CMD line prompt. Ex:
Jon Done
Test001
Tester002
Test003
Tester004
Can anyone help me to do this?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN ('dir /b /a-d 2^>nul') DO ECHO %%~fa
FOR /f "delims=" %%a IN ('dir /b /ad') DO (
PUSHD "%%a"
FOR /f "delims=" %%x IN ('dir /b /a-d 2^>nul') DO ECHO %%~fx
popd
)
popd
GOTO :EOF
This should work - you would need to change the setting of sourcedir to suit your circumstances.

Get inverse list of files through dir command in batch

I'm triying to get a list of files of subfolders in a inverse order using this:
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f)
I'm getting this result
folder1\file2, folder1\file, folder2\file2, folder2\file1, folder3\file2, folder3\file1
And I want to get the reverse order in folders and files, not only files of folders. Something like this
folder3\file2, folder3\file1, folder2\file2, folder2\file1\, folder1\file2, folder1\file1
How can I do this?
#echo off
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f >> tempfile.123)
C:\Windows\System32\sort.exe /R tempfile.123
del tempfile.123
This just echoes your files to tempporary file and then revereses it. Sorry for the full path to sort.exe but I have cygwin installed. In case you want proper temporary file name I reccomend this http://unserializableone.blogspot.com/2009/04/create-unique-temp-filename-with-batch.html
try this
for /f "tokens=*" %%f in ('dir /s /b /o-n')
do (
SET OUTPUT=%OUTPUT%, %%f
)
echo %OUTPUT%
run a first loop to get first level subdir then run you command on each of them
#echo off
for /f "tokens=*" %%f in ('dir c:\temp\so\ /b /o-n /AD') do (call :filesOf %%f)
:next
echo next
pause
:filesOf
echo "files for ******** %1 *********"
if ("%1"=="") goto next else(
for /f "tokens=*" %%i in ('dir c:\temp\so\%1 /s /b /o-n ') do echo "***%%i***"
)
it will be difficult to handle multi level subdirs tough

Batch file to delete specific text file from multiple locations

I have a .bat file written that creates a text file within each sub-folder of the root folder that displays the sub-folder's contents. The issue I'm having now is creating a second .bat that will remove said text files from the sub-folder locations. The .bat I have for creating the .txt files are located below. Any help is greatly appreciated!
#echo off
IF EXIST "R:\Projects\000" PUSHD "R:\Projects\000\"
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') do (
dir /s/b > R:\Projects\000\%%G\Folder_Contents.txt
)
POPD
It's nearly the same as your current script.
delAllFolderContent.bat
#ECHO OFF
IF EXIST "R:\Projects\000" (
PUSHD "R:\Projects\000"
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') DO (
DEL "R:\Projects\000\%%G\Folder_Contents.txt"
)
POPD
)
I moved the FOR/F code into the IF EXIST block, so it will be executed only when the directory exists and not always.
If i truly understand you. It's look like this:
#ECHO OFF
IF EXIST "C:\Temp\" PUSHD "C:\Temp\"
ECHO #ECHO OFF > loc.bat
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') DO (
DIR /s/b > C:\Temp\%%G\Folder_Contents.txt
ECHO DEL C:\Temp\%%G\Folder_Contents.txt >> loc.bat
)
POPD
del "R:\Projects\000\folder_contents.txt" /s

How can I copy files to the "newest subdir" in a directory using a batch file?

I need to copy files from recent build folder to another folder used for testing. I'm having a hard time getting the name of the most recent build folder.
My current attempt is this:
#for /D %%i in ('dir e:\builds\projectA\* /O:D') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s
I was hoping target would be the newly created folder from which I could then copy the files from. However, when I echo target to the console it just says:
/O:D'
Does anyone know how I can get this to work (or know of an alternative)?
Replace the /D with /F and add /B to the bracketed dir command.
#for /F %%i in ('dir e:\builds\projectA\* /O:D /B') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s
pushd E:\builds\projectA
for /f "delims=" %%d in ('dir /b /a:d /o:d') do #echo %%d>latest.txt
for /f "delims=" %%l in (latest.txt) do xcopy "%%l\*.*" \\devbox\projectA /y /s
del latest.txt
popd

Resources