batch create symlinks of all files/folders in a directory - batch-file

I have a directory with many files and folders and I'd like to make symlinks of all files and folders in that directory to another folder but exclude one folder
Any suggestions?

#echo off
set source=c:\source\directory
set target=c:\target\directory
set exclude=DoNotLinkThisDirectory
forfiles /P "%source%" /C "cmd /c if #isdir==TRUE (if not #file==\"%exclude%\" mklink /d \"%target%\#file\" #path ) else ( mklink \"%target%\#file\" #path )"
EDIT - Updated to allow "easily" add of multiple excludes, using /G:file if findstr command filter the file/folder list
#echo off
set "source=c:\source\directory"
set "target=c:\target\directory"
set "exclude=%temp%\exclude.txt"
(
rem exclude files/dires with these strings into full path
echo .txt
echo pipe.cmd
rem escaped backslash and initial and final quotes to avoid partial matches
echo "c:\\source\\directory\\something.txt"
rem exclude thisNot file/directory from source directory
echo "%source:\=\\%\\thisNot"
)> "%exclude%"
forfiles /P "%source%" /C "cmd /c (echo #path|findstr /i /v /g:"%exclude%" >nul) && if #isdir==TRUE (mklink /d \"%target%\\\"#file #path) else (mklink \"%target%\\\"#file #path)"
del "%exclude%" > nul

Related

How to record purged files into a log file

I am using the following code in a batch file to purge any files older than 60 days. I would like the log file to print the file names that have been purged for record keeping purposes. How can I do that?
setlocal EnableDelayedExpansion
set logpath=C:\Temp\Archive
REM if not exist %logpath% md %logpath%
set FILEAGE=60
set archlog=%logpath%\Accurate_ARCHIVE.txt
set inputdir=C:\Temp
REM Set the date
FOR /f "tokens=2,3,4 delims=/ " %%a in ('date/t') do (set DD=%%a) & (set MM=%%b) & (set YYYY=%%c)
REM * Purge files older than %FILEAGE% days from disk.
forfiles /p "%inputdir%" /s /m *.* /d -%FILEAGE% /c "cmd /c del #path"
ECHO Folder %inputdir% was processed on %DD%-%MM%-%YYYY% >> %archlog%
ECHO Files older than %FILEAGE% days have been purged. >> %archlog%
exit %errorlevel%
I am expecting the log file to print the names of the files purged.
Add echo #path to the del #path command.
Something like below (untested).
"cmd /c (del #path) & (>> %archlog% echo #path)"
A more advanced version incorporating comments by Aschipf and Compo.
The 0x22 puts doupble quots around the filename and prevents problems with space and other special characters in the filename.
Deleted files are reported and files that could not be deleted are reported prefixed with NOT DELETED:.
"cmd /c if #isdir==FALSE (del #path) & (>> 0x22%archlog%0x22 if exist #path (echo NOT DELETED: #path) else (echo #path))"

Batch Delete excluding files under specific folder (or file names with specific pattern /wildcard e.g. Test123.csv, Test623.csv, Test854.csv)

FORFILES /P "C:\Temp\" /D -3 /S /C "cmd /c if #isdir==FALSE del /F /Q #path"
above script is working fine and delete all files under Temp and its subdirectory older than 3 days.
I want to exclude all files from specific folder say all files from folder XYZ or full path-> C:\Temp\ABC\XYZ
Note : all files under XYZ folders are having pattern say Test*.*.csv
forfiles does not have an exclude option. You have to use something like findstr /V to exclude the results, but that will not form part of the forfiles command in itself. We simply incorporate a for loop and ecxlude using findstr /V, then delete:
#echo off
for /f "delims=" %%i in ('FORFILES /P "%temp%" /D -3 /S /C "cmd /c if #isdir==FALSE echo #path"^| findstr /VI "C:\Temp\ABC\XYZ"') do del /F /Q "%%~i"

Unable to Delete empty folders of directory and sub-directory using batch

with the help of batch file I am able to delete files from the main directory and sub directories but I am finding it difficult to delete empty sub directories left after files inside it got deleted . I have written script but it is not working for deleting empty sub directories part
MY Batch Script
#echo off
Set _Extentions=".LOG"
Set _FolderList= "D:\FCS"
Set _FolderList=%_FolderList% "D:\SMSGateway\SMSLogs"
Set _NoOfDays=15
for %%s in (%_FolderList%) do (
for %%d in (%_Extentions%) do (
call :process_Deletion %%s %%d
)
)
:process_Deletion
IF [%1] == [] GOTO EndOfFun
set _ParaPath=%1
set _ParaExtn=%2
echo forfiles /P %_ParaPath% /S /m *%_ParaExtn% /D -%_NoOfDays% /C "cmd /c if #isdir==FALSE echo #path #fdate #ftime"
echo forfiles /P %_ParaPath% /S /m *%_ParaExtn% /D -%_NoOfDays% /C "cmd /c if #isdir==FALSE del #file"
echo for /f "delims=" %%d in ('dir %_FolderList% /s /b /ad ^| sort /r') do rd "%%d"
:EndOfFun
EXIT /B
What am I doing wrong here ? Please help..
Empty directories require rd or rmdir
https://www.computerhope.com/rmdirhlp.htm
The rd and rmdir commands remove empty directories in MS-DOS. To delete directories with files or directories within them, you must use the deltree command. If you are running Microsoft Windows 2000 or Windows XP, use the /S option.

How to I delete from all Sub Directories apart from a select few

I would like to create 1 batch file that deletes a type of file (i.e. ".bak") with different date ranges situated in different sub folders that are in the same patent folder, but I can list the minority of folders.
I have managed to create a FOR loop to look in certain directories (which the directories are set in a variable), deleting any '*.bak' file over a certain age with the below script:
SET "WeeklyBAKLocation=Folder20 Folder21 Folder25 Folder28"
SET WeeklyBAK=7
SET DailyBAK=3
FOR %%x IN (%WeeklyBAKLocation%) DO forfiles /p "%%x" /s /m *.bak /d -%WeeklyBAK% /c "cmd /c DEL #path /q"
<SECOND FOR LOOP below HERE>
The above works deleting .bak files older than 7 days in those folders, my question is how can I reverse that and delete '.bak' files in every other directory that's older than 3 days, without deleting the ones kept from the first query?
I tried nesting FOR and FORFILES, using findstr /v to ignore them as below (but it would ignore each directory for that 1 loop, and then delete it for the next loop wouldn't it, so eventually it would not have worked and is massively inefficient?)
SET "WeeklyBAKLocation=Folder20 Folder21 Folder25 Folder28"
SET WeeklyBAK=7
SET DailyBAK=3
<First FOR LOOP above HERE>
FOR %%x IN (%WeeklyBAKLocation%) DO FOR /F "delims=*" %%G IN ('dir /b /s "%~dp0" ^| findstr /v "\%%x"') DO forfiles /p "%%G" /s /m *.bak /d -%DailyBAK% /c "cmd /c ECHO #path /q"
Essentially can I ignore SOME directories at once when walking through them to find .bak files to delete only ones that are 3 days old.
Many thanks in advance.
FOR /F "delims=*" %%G IN (
'dir /b /s /AD "%~dp0" ^| findstr /v "\%%x"') DO (
set "zapme=Y"
FOR %%x IN (%WeeklyBAKLocation%) DO if /i "%%x"=="%%G" set "zapme="
if defined zapme forfiles /p "%%G" /s /m *.bak /d -%DailyBAK% /c "cmd /c ECHO #path /q"
)
read the directory list in basic form (note /ad) and for each directory, set zapme to something then check whether the directory is not to be processed and set zapme to nothing if it's one of those directories. The zapme flag can then be tested for being set or not set. If it's set, go ahead and process the directory.
FOR /F "delims=*" %%G IN (
'dir /b /AD "%sourcedir%"') DO (
set "zapme=Y"
FOR %%x IN (%WeeklyBAKLocation%) DO if /i "%%x"=="%%G" set "zapme="
if defined zapme ECHO forfiles /p "%sourcedir%\%%G" /s /m *.bak /d -%DailyBAK% /c "cmd /c ECHO #path /q"
)
...happens every time I don't actually test it...

forfiles has space in folder path

I want to try the bat file to copy my log to other space.
My code:
REM get date
FOR /F "tokens=1-4 delims=/ " %%a IN ("%date%") DO (SET _MyDate=%%d/%%b/%%c)
echo _MyDate: %_MyDate%
set _path= "D:\Logs\AddressBook Service"
set _path2= "E:\Logs_test\AddressBook Service"
forfiles /p %_path% /d -%_day% /m *.log /c "cmd /c xcopy #path %_path2%"
My question is How to solve the forfiles #path has spaces in folder path?
Like this: E:\Logs_test\AddressBook Service
set "_day=2" only my guess
set "_path=D:\Logs\AddressBook Service"
set "_path2=E:\Logs_test\AddressBook Service"
forfiles /p "%_path%" /d -%_day% /m *.log /c "cmd /c echo xcopy #path \"%_path2%\\\" /D /E /-Y"
Above code snippet with proper escaped " double quotes (surprisingly escaped using a backslash \" instead of common caret) should result in something like
xcopy "D:\Logs\AddressBook Service\some name.log" "E:\Logs_test\AddressBook Service\" /D /E /-Y
Note that operational xcopy is merely displayed for debugging purposes. Remove echo keyword no sooner than debugged.
Adding a trailing backslash to xcopy target folder (%_path2%\\) seems to be facultative (optional) supplement.
Finally, note double quotes in set "variablename=variable value" syntax.

Resources