how to create a joint list of files in a 2 folders - batch-file

BACKGROUND
I'm trying to create a [joint] list of (just) the file names from two different folders, namely FULL_REMOTE_DIR and FULL_LOCAL_DIR
CODE
#echo off
SETLOCAL enabledelayedexpansion
TITLE Demo Mode
ECHO small script to showcase
ECHO how to iterate folders
SET mypath=%cd%
SET "remote_dir=\remote\*"
SET "local_dir=\local\*"
SET "other_dir=\other\*"
SET "FULL_REMOTE_DIR=%mypath%%remote_dir%"
SET "FULL_LOCAL_DIR=%mypath%%local_dir%"
SET "FULL_OTHER_DIR=%mypath%%other_dir%"
ECHO REMOTE DIR:
FOR %%a IN ("%FULL_REMOTE_DIR%") DO (
SET REMOTE_DIR_LIST=%%a
ECHO %%a)
ECHO LOCAL DIR:
FOR %%a IN ("%FULL_LOCAL_DIR%") DO ECHO %%a
ECHO OTHER DIR:
FOR %%a IN ("%FULL_OTHER_DIR%") DO ECHO %%a
QUESTION
How can I create a list of files that are in FULL_REMOTE_DIR and FULL_LOCAL_DIR

Try this:
SETLOCAL EnableDelayedExpansion
cd /D %~dp0
set _filelist=,
for /f "delims=|" %%f in ('dir FULL_REMOTE_DIR_PATH /b %CD%') do (
set "_filelist=!_filelist!,%%f"
)
set _filelist=%_filelist:,,=%
echo %_filelist%
You need to run it from the FULL_LOCAL_DIR, it should create a list of FULL_LOCAL_DIR + FULL_REMOTE_DIR.

Related

batch to display folders to select from, user selects folder, then it copies that folder to another folder

So what I'm trying to do is, i have a bat file that makes timestamped backups(for example, 180126_053327 in the format: yymmdd_hhmmss). I'm trying to create this bat to add to it, so i can have it lookup those backups, display in console to allow the user to select the backup they want to restore by inputting the number they want and then copy it to a location.
so far what i have creates this:
Pic of what I have so far
The pic above is from the following script:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set i=0
For /f %%a in ('dir I:\AM_Configs-backups\ /B /A /D') do (
set /a i+=1
echo !i! %%a
set dir!i!=%%a
)
echo.
set /p uin="Select a directory [1-!i!]: "
set udir=!dir%uin%!
echo Selected - %udir%
md c:\test2
copy %udir% c:\test2
#PAUSE
I keep getting this:
Select a directory [1-29]: 27
Selected - 180126_053327
The system cannot find the file specified.
Press any key to continue . . .
I got the script above from this link: Prompting user to select directory in batch file
It cannot be found because %udir% is not in the current directory. This means that your base directory, I:\AM_Configs-backups, needs to be pre-fixed to your Copy command.
Copy "I:\AM_Configs-backups\%udir%" "C:\test2"
Also here is an alternative to the linked example you provided in your question:
#Echo Off
SetLocal EnableDelayedExpansion
Set "baseDir=I:\AM_Configs-backups"
For /F "Delims==" %%A In ('"(Set dir[) 2>Nul"') Do Set "%%A="
Set "i=0"
For /D %%A In ("%baseDir%\*") Do (Set /A i+=1 & Set "dir[!i!]=%%~nxA")
If %i% Equ 1 (Set "dir[X]=%baseDir%\%dir[1]%") Else Call :Menu
Rem Your commands using the selected directory begin below
Echo you selected %dir[X]%
If Not Exist "C:\test2\" MD "C:\test2"
Copy "%dir[X]%" "C:\test2"
Pause
Exit /B
:Menu
For /L %%A In (1,1,%i%) Do (Echo %%A. !dir[%%A]!)
Set /P "dir[X]=Select a directory from the above list: "
If Not Defined dir[%dir[X]%] (ClS & GoTo Menu)
Set "dir[X]=%baseDir%\!dir[%dir[X]%]!"
You can adjust your base directory on line 3 and put your own commands from line 9 onwards, (up to the Exit /B).
You should consider using RoboCopy for copying directories!
Edit
For the XCopy version, you'd probably need to change to:
#Echo Off
SetLocal EnableDelayedExpansion
Set "baseDir=I:\AM_Configs-backups"
For /F "Delims==" %%A In ('"(Set dir[) 2>Nul"') Do Set "%%A="
Set "i=0"
For /D %%A In ("%baseDir%\*") Do (Set /A i+=1 & Set "dir[!i!]=%%~nxA")
If %i% Equ 1 (Set "dir[X]=%dir[1]%") Else Call :Menu
Rem Your commands using the selected directory begin below
Echo you selected %dir[X]%
XCopy "%baseDir%\%dir[X]%" "%AppData%\Awesomeminer\%dir[X]%" /S /I /F /Y 2>Nul
Echo Starting Awesome Miner...
Start "" "%ProgramFiles(x86)%\Awesome Miner\AwesomeMiner.exe"
Pause
Call "Switch-N-Bkup.bat"
Pause
Exit /B
:Menu
For /L %%A In (1,1,%i%) Do (Echo %%A. !dir[%%A]!)
Set /P "dir[X]=Select a directory from the above list: "
If Not Defined dir[%dir[X]%] (ClS & GoTo Menu)
Set "dir[X]=!dir[%dir[X]%]!"
For the RoboCopy version you'd change line 11 to:
RoboCopy "%baseDir%\%dir[X]%" "C:\test2\%dir[X]%" /S 2>Nul
...plus any additional switches you may need
I ended up using this cus i have another batch for switching and backing up:
to kinda give you an idea, i have a primary bat for switching and copying configs, which will start teh program automatically after their copied. in that bat i also have code for backing up current configs to a location using time/date and creates the folders in this format: yymmdd_hhmmss...i needed an easy restore function, which with the link and code you provided helped with the user able to select which folder to restore and copying it to the programs data folder to be used, then auto-starting the program using the copied config.
#Echo Off
SetLocal EnableDelayedExpansion
Set "baseDir=I:\AM_Configs-backups"
For /F "Delims==" %%A In ('"(Set dir[) 2>Nul"') Do Set "%%A="
Set "i=0"
For /D %%A In ("%baseDir%\*") Do (Set /A i+=1 & Set "dir[!i!]=%%~nxA")
If %i% Equ 1 (Set "dir[X]=%baseDir%\%dir[1]%") Else Call :Menu
Rem Your commands using the selected directory begin below
Echo you selected %dir[X]%
REM If Not Exist "C:\test2\" MD "C:\test2"
xcopy "%dir[X]%" "%USERPROFILE%\AppData\Roaming\Awesomeminer\" /S /F /R /Y
START C:\"Program Files (x86)"\"Awesome Miner"\AwesomeMiner.exe
ECHO Starting Awesome Miner...
#Pause
CALL Switch-N-Bkup.bat
exit /b
:Menu
For /L %%A In (1,1,%i%) Do (Echo %%A. !dir[%%A]!)
Set /P "dir[X]=Select a directory from the above list: "
If Not Defined dir[%dir[X]%] (ClS & GoTo Menu)
Set "dir[X]=%baseDir%\!dir[%dir[X]%]!"

Windows Batch - Find all pdf's in sub directories, exclude specific foldernames

Context
I'm currently programming with batch files, to use a specific pdf tool only available for cmd.
Problem
I'm trying to run a for loop, which recursively cycles through a directory finding all *.pdf files. Excluding the pdf's inside folders named "Originals"
If the pdf file is in a parent folder named "Originals", then it must be skipped. Otherwise count the pdf file with the %counter% variable.
Example Directory Structure
C:\New folder\file (1).pdf
C:\New folder\file (2).pdf
C:\New folder\Sub_1\file (1).pdf
C:\New folder\Sub_1\file (2).pdf
C:\New folder\Sub_1\file (3).pdf
C:\New folder\Sub_2\file (4).pdf
C:\New folder\Sub_2\file (5).pdf
C:\New folder\Originals\file (1).pdf
C:\New folder\Originals\file (2).pdf
Example batch file - Find all pdf's (excluding Originals)
:: Example.bat
#echo off
set myDirectory=C:\New folder
:: Search through myDirectory to find all .pdf files (including subdirectories)
setlocal enableDelayedExpansion
for /R "%myDirectory%" %%G in (*.pdf) do (
set inputDirectory=%%~dpG
echo G = !%%G!
echo inputDirectory = !inputDirectory!
for /f "delims=\" %%F in ("!inputDirectory!") do (
set currentFolder=%%~nxF
echo currentFolder = !currentFolder!
)
if NOT "!currentFolder!"=="Originals" (
set /a count=count+1
)
)
echo There are %count% PDF's (excluding originals)
pause
Please run the example batch file to demonstrate what I have so far. Any help or solutions would be appreciated.
Cheers!
Solved
Here's what I came up with based on everyone's solutions!
#echo off
setlocal enabledelayedexpansion
set count=0
set myDirectory=C:\New folder
for /r "%myDirectory%" %%i in (*.pdf) do (
set inputDirectory=%%~dpi
set inputDirectoryNoSlash=!inputDirectory:~0,-1!
for %%j in (!inputDirectoryNoSlash!) do set sub=%%~nxj
if NOT !sub!==Originals (
set /a count=count+1
)
)
echo There are %count% PDF's (excluding originals)
pause
Thanks again guys!
Another similar method is:
#echo off
setlocal enabledelayedexpansion
set count=0
set dir=C:\TEST
echo %count%
for /r "%dir%" %%i in (*.pdf) do (
set dirx=%%~dpi
set con=!dirx:~0,-1!
for %%j in (!con!) do set sub=%%~nxj
if !sub!==Originals (
cls
) else (
set /a count=count+1
)
)
echo !count!
pause
*Change to appropriate pathname
%%~pG Expand %%G to a Path only including a trailing \ backslash. Undesired, clear it away as follows:
for /f "tokens=* delims=\" %%F in ("!inputDirectory:~0,-1!") do (
set currentFolder=%%~nxF
echo currentFolder = !currentFolder!
)
Try this:
#echo off
setlocal EnableDelayedExpansion
set myDirectory=C:\New folder
set count=0
for /F "delims=" %%a in ('dir /S "%myDirectory%\*.pdf" ^| findstr /V /L "\Originals\"') do (
echo File: %%a
set /A count+=1
)
echo There are %count% PDF's (excluding originals)
pause

Change File Name to Folder Name

I've been working on a batch script (.bat) to change file name to the same folder name 3 levels up but no luck. I can only get it to rename 1 level up. There are multiple files in multiple folders.
Any help would be greatly appreciated.
I don't completely understand the intent of your current code. But the following will rename a given file based on the folder 3 levels up. Note that you can only rename one file with a given extension per folder using this strategy.
#echo off
setlocal
set "file=H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\abc.avi"
for %%A in ("%file%") do for %%B in ("%%~A\..\..\..") do ren "%%~A" "%%~nxB%%~xA"
Note that I include the extension of the folder name because folder names can include dots.
EDIT
Based on OP's comments, I believe the following will properly rename all relevant .avi files. The code has been tested, and works in my hands. Simply set the root and files values as appropriate.
#echo off
setlocal
set "root=H:\Techs\Exported Videos\Scenario"
set "files=*.avi"
for %%A in ("%root%\.") do set "root=%%~fA"
for /f "delims=" %%A in ('dir /b /s /a-d "%root%\%files%"') do (
for %%B in ("%%~A\..\..\..") do (
if /i "%%~dpB" equ "%root%\" ren "%%~A" "%%~nxB%%~xA"
)
)
you might try this:
#echo off &setlocal
set /a PathLevel=3
for %%a in (
"H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi"
"H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi"
) do (
call:doit "%%~a"
)
goto:eof
:doit
set "fname=%~1"
set /a cnt=0
for %%a in ("%fname:\=","%") do set /a cnt+=1
set /a cnt-=PathLevel
for %%a in ("%fname:\=","%") do (
set /a cnt-=1
setlocal enabledelayedexpansion
if !cnt! equ 0 (
endlocal
set "nname=%%~a"
) else endlocal
)
echo ren "%fname%" "%nname%%~x1"
goto:eof
output is:
ren "H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi" "1-CAS IED.avi"
ren "H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi" "2-SAF.avi"

use DIR command output in batch

I use command :
dir D:\ /b /A:D-H
and output is for example:
Photos
Wallpapers
My Personal Data
and.. I want to set varialeble on this folders as:
set SOMEFOLDER=folder1
set SOMEFOLDER2=folder2
and ect..
How can I do this?
You can use the following for a batch file in the same directory as the folders:
#echo off
setlocal enabledelayedexpansion
set num=0
for /d %%i in (*) do set /a num+=1&set SOMEFOLDER!num!=%%i
echo SOMEFOLDER1 = %SOMEFOLDER1%
echo SOMEFOLDER2 = %SOMEFOLDER2%
pause
If you need the batch file in a different directory or want to change some options, use this instead:
#echo off
setlocal enabledelayedexpansion
set num=0
for /f "tokens=*" %%i in ('dir D:\ /b /a:D-H') do set /a num+=1&set SOMEFOLDER!num!=%%i
echo SOMEFOLDER1 = %SOMEFOLDER1%
echo SOMEFOLDER2 = %SOMEFOLDER2%
pause

How to open numerous folders and replace all the files contents using batch files

I am new to writing batch files.Can anyone tell me how to open all the folders from root folder and open all the files in it and change their content. Means if "C:\abc\xyz" now all the files in 'abc' folder should get their contents changed and then all the files in 'xyz' folder. Now I do have code for changing file contents. Kindly tell me how to get into folders and sub folders and get all the files changed ...
CODE TO REPLACE STRING IN FILES
#echo off
setlocal enabledelayedexpansion
set INTEXTFILE=b.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=path
set REPLACETEXT=hello
set OUTPUTLINE=
for /f "tokens=1,* delims=ΒΆ" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
With following code I do get all files in D:
#echo off
IF EXIST D:\*.* goto process
echo does not exist
:process
for %%a in (D:\*.*) do echo %%a
AND with by appling '/d' I do get all folders
for /d %%a in (D:\
.) do echo %%a
However I am not able to use them together ...I want all files from all folders
Read HELP FOR, then try FOR /R %A IN (*.TXT) DO ECHO %A
then modify your batch file accordingly, adding something like this
FOR /R %%A IN (*.TXT) DO (
ECHO %%A
FOR /F %%B in (%%A) DO (
ECHO %%B
)
)

Resources