cmd bat file, allow access to network share - batch-file

I have the below code which works excellently to compare 2 latest files and output the difference into a separate folder However in the :getLatestFileInFolder I need it to access the SAN network location. Currently it can only access local. Can someone tweak this code for me please. The location I need to access is on the san "\3663vfas01\Biztalk$\Live"
#echo off
cd /d C:\Users\test\Important Structure\Development\AX copy
for /f %%a in ('dir /b /od /a-d /tw') do (set latest=%%a)
setlocal enableextensions disabledelayedexpansion
call :getLatestFileInFolder "C:\Users\test\Important Structure\Development\AX copy" latestC
call :getLatestFileInFolder "C:\Users\test\Important Structure\Development\Reflex copy" latestD
if not defined latestC ( echo NO File in C & exit /b )
if not defined latestD ( echo NO File in D & exit /b )
for /f "tokens=1,*" %%a in (
'diff "%latestC%" "%latestD%" ^| findstr /r /c:"^<" /c:"^>"'
) do (
>> "C:\Users\test\Important Structure\Development\Error\%latest%" echo(%%b
)
endlocal
exit /b
:getLatestFileInFolder folderToSearch variableToReturn
setlocal
set "folder=%~1" & if not defined folder set "folder=%cd%"
set "latest="
pushd "%folder%"
for /f "tokens=*" %%a in ('dir /b /o-d /a-d /tw 2^>nul') do (set "latest=%%~fa" & goto :latestFileFound)
:latestFileFound
popd
endlocal & set "%~2=%latest%" & goto :eof

EDITED
Change this line:
set "fC=C:\Users\test\Important Structure\Development\AX copy"
to this:
set "fC=\\3663vfas01\Biztalk$\Live"
in this code, which should function as yours was doing:
#echo off
set "fC=C:\Users\test\Important Structure\Development\AX copy"
set "latestC="
for /f "delims=" %%a in ('dir "%fC%" /b /od /a-d /tw 2^>nul') do set "latest=%%a" & set "latestC=%fC%\%%a"
set "fD=C:\Users\test\Important Structure\Development\Reflex copy"
set "latestD="
for /f "delims=" %%a in ('dir "%fD%" /b /od /a-d /tw 2^>nul') do set "latestD=%fD%\%%a"
if not defined latestC ( echo NO File in C & exit /b )
if not defined latestD ( echo NO File in D & exit /b )
for /f "tokens=1,*" %%a in (
'diff "%latestC%" "%latestD%" ^| findstr /r /c:"^<" /c:"^>"'
) do (
>> "C:\Users\test\Important Structure\Development\Error\%latest%" echo(%%b
)

If you mean that you need to do the equivalent of a cd command to a network path then you can use pushd for this, which temporally assigned a mapped drive e.g.
pushd "\\3663vfas01\Biztalk$\Live"

Related

batch script for deleting all but the newest folder and ignoring one specific folder

#echo off
setlocal
set "workdir=C:\orbis32\"
set "folder="
for /f "tokens=* delims=" %%i in ('dir %workdir% /AD /B /TW /O-D') do (
set "folder=%%~fi"
goto :break
)
:break
echo newest... %folder%
for /f "skip=1 tokens=* delims=" %%i in ('dir %workdir% /AD /B /TW /O-D') do (
echo rd /s /q "%%~fi"
)
pause
now i need to exclude one specific Folder (orbis32\logs) from this but im currently stuck
No need to first find the newest folder (by the way: it could be the logs folder) - skip is sufficient. Just filter out the obis32\logs lines from the dir command:
#echo off
setlocal
set "workdir=C:\orbis32\"
set "folder="
for /f "skip=1 delims=" %%i in ('dir %workdir% /AD /B /TW /O-D ^|find /i /v "orbis32\logs"') do (
echo rd /s /q "%%~fi"
)
pause

Opening a random folder from a set directory

How can I create a batch script that opens a random folder within a specific directory? This code here prints out a randomly chosen file(I need it to open the folder, not the file) but I could not figure out how to open it.
#Echo Off
:Start
set directory="D:\Movies"
set count=0
for /f %%f in ('dir "%directory%" /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0
for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /a-d /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J
:Found
echo %filename%
pause
goto Start
I suddenly realised what I was doing wrong and solved the problem. Here is the final and working code:
#Echo Off
:Start
set directory="D:\Film"
set count=0
for /f %%f in ('dir "%directory%" /ad /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0
for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /ad /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J
:Found
%SystemRoot%\explorer.exe %filename%
exit /b
goto Start
You really shouldn't need to increment a count and use findstr for such a task; just assigning and sorting a random number should do:
#Echo Off
Set "source=D:\Film"
SetLocal EnableDelayedExpansion
For /D %%A In ("%source%\*") Do Set "$[!RANDOM!]=%%A"
For /F "Tokens=1* Delims==" %%A In ('"Set $[ 2>Nul|Sort"'
) Do Set "target=%%B" & GoTo Found
Exit /B
:Found
Explorer "%target%"
If you wanted a recursive directory search then change line 5 to:
For /D /R "%source%" %%A In (*) Do Set "$[!RANDOM!]=%%A"

Detecting characters batch sript

I have a script which is renaming and moving files to folder by detecting its character based on _ delimeter and the numbers listed below.
631598_S_14CG_Layout-1.pdf
631598_1_Barcode.pdf
631598_001_CutData.ppf
Output folder should be 631598_S_1
631598_S_14CGBarcode.pdf
631598_001_CutData.ppf
Output folder should be 631598_S_2
631598_S_14CG_Layout-11.pdf
631598_11_Barcode.pdf
631598_011_CutData.ppf
Output folder should be 631598_S_11
631598_S_14CG_Layout-22.pdf
631598_22_Barcode.pdf
631598_22_CutData.ppf
Output folder should be 631598_S_22
But the thing is I am on only detecting 10 characters and so all those who has the same 10th charater moved to the same folder for example:
631598_S_14CG_Layout-22.pdf
631598_22_Barcode.pdf
631598_22_CutData.ppf
…will move to 631598_S_2 and not 631598_S_22
Below is the script
#echo off
setlocal enabledelayedexpansion
set "Pattern=_Layout-"
set "Replace=_"
for %%a in (*.pdf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern%=%Replace%!"
)
for /f "tokens=1-4 delims=_." %%a in ('dir /b *.pdf') do ren "%%a_%%b_%%c_%%d.pdf" "%%a_%%b_%%d.pdf"
pushd ."\*"
for /f "delims=" %%F in ('dir /b /a-d *.pdf ^|findstr /v "Barcode"') do (
2>nul md "%%~nF"
)
TIMEOUT /T 01
set "Pattern1=_00"
set "Replace1=_S_"
for %%a in (*.ppf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern1%=%Replace1%!"
)
set "Pattern2=_0"
set "Replace2=_S_"
for %%a in (*.ppf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern2%=%Replace2%!"
)
set "Pattern3=Cut Data"
set "Replace3=CutData"
for %%a in (*.ppf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern3%=%Replace3%!"
)
set "Pattern=_Barcode"
set "Replace=_Barcode_S"
for %%a in (*.pdf) do (
set "File=%%~a"
ren "%%a" "!File:%Pattern%=%Replace%!"
)
for /f "tokens=1-4 delims=_." %%a in ('dir /b *.pdf') do ren "%%a_%%b_%%c_%%d.pdf" "%%a_%%d_%%b_%%c.pdf"
for /f "tokens=*" %%1 in ('dir /a-d /b *.pdf *.ppf') do (
set filename=%%1&set dirname=!filename:~0,10!
for /f "tokens=*" %%A in ('dir /ad /b') do (
set dirid=%%A&set dirid=!dirid:~0,10!
if "!dirid!" equ "!dirname!" move %%1 %%A
)
)

How can I create a log file?

How to save a log .txt file of all deleted files with this code?
Also I need to change code to search for all backup folders in root directory subfolders.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
if not "!LastDate!" == "%%I" (
for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do rd /Q /S "%BackupFolder%\%%D"
set "LastDate=%%I"
)
)
endlocal
This code was written by Mofi and posted as answer on How to remove oldest folder(s) of a group of folders in a folder with several folder groups? He posted also an enhanced version logging the files before deletion, but it was not working for me properly.
I tried with the followed command >> but log.txt file has not been generated.
The attempt to use >> was not shown in the question. How about something like:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
SET "LOGFILE=%TEMP%\%~n0.log"
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
if not "!LastDate!" == "%%I" (
for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do (
DIR /S /B "%BackupFolder%\%%D" >>"%LOGFILE%"
rd /Q /S "%BackupFolder%\%%D"
set "LastDate=%%I"
)
)
)
endlocal

batch file command to compare newest file and output differences

I have the below code which someone gave to me but I don't know how to put it together in a bat file so it runs successfully.
The aim is to find the latest (last modified) file in c:/ and compare it with c:/2.txt and output the differences into c:/786.txt
cd /d c:\
for /f %%a in ('dir /b /o-d /a-d /tw') do (set latest=%%a & goto :eof)
for /f "tokens=1*" %%a in (
'diff c:\%latest% c:\2.txt ^| findstr /r /c:"^<" /c:"^>"'
) do #echo %%b >>c:\786.txt
Can someone please put this code together for me.
cd /d c:\
set "latest="
for /f %%a in ('dir /b /o-d /a-d /tw') do (set "latest=%%a" & goto :found)
:found
if not defined latest exit /b
for /f "tokens=1,*" %%a in (
'diff "c:\%latest%" "c:\2.txt" ^| findstr /r /c:"^<" /c:"^>"'
) do (
>> "c:\786.txt" echo(%%b
)
Ordering by date descending, the latest file is the first, so on first iteration assign the file name and exit of the for loop.
Then check if any file has been found. It not, end of the script
If we have a file, compare latest file against the indicated one and send the filtered lines to the final file.
EDIT - Refactor code to made it more usable and adapt to comments. Search for last file in folder moved to a subroutine.
#echo off
setlocal enableextensions disabledelayedexpansion
call :getLatestFileInFolder "c:\" latestC
call :getLatestFileInFolder "d:\" latestD
if not defined latestC ( echo NO File in C & exit /b )
if not defined latestD ( echo NO File in D & exit /b )
for /f "tokens=1,*" %%a in (
'diff "%latestC%" "%latestD%" ^| findstr /r /c:"^<" /c:"^>"'
) do (
>> "c:\786.txt" echo(%%b
)
endlocal
exit /b
:getLatestFileInFolder folderToSearch variableToReturn
setlocal
set "folder=%~1" & if not defined folder set "folder=%cd%"
set "latest="
pushd "%folder%"
for /f "tokens=*" %%a in ('dir /b /o-d /a-d /tw 2^>nul') do (set "latest=%%~fa" & goto :latestFileFound)
:latestFileFound
popd
endlocal & set "%~2=%latest%" & goto :eof

Resources