i was trying to copy a files modified today from one shared drive another shared drive.
but when i execute the batch file, it displays the following error.
cmd does not support unc paths as current directories
the script is as below
#echo off
Set PhotosrcPath=\\hqcp-appsvr01\Files\ApplicationDocuments\AppDocs\49\PHOTO\
Set PhotodestPath=\\hqcp-appsvr02\Files\ApplicationDocuments\AppDocs\49\PHOTO\
::Copy photoFiles from server1 to server2 Made Or Modified Today
#echo off
set source=%d%%PhotosrcPath%
set dest=%d%%PhotodestPath%
pushd "%source%"
set t=%date:~4%
echo %t%
for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
goto :eof
popd
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" Xcopy /y %1 "%dest%"
goto :eof
net use X: \\hqcp-appsvr01\Files\ApplicationDocuments\AppDocs\49\PHOTO
net use Y: \\hqcp-appsvr02\Files\ApplicationDocuments\AppDocs\49\PHOTO
::Copy photoFiles from server1 to server2 Made Or Modified Today
#echo off
set source=X:\
set dest=Y:\
pushd "%source%"
set t=%date:~4%
echo %t%
for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
popd
net use Y: /d /y
net use X: /d /y
goto :eof
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" Xcopy /y %1 "%dest%"
goto :eof
Something like that might work. Look up NET USE for more information.
I'm curious why you aren't just using XCOPY - I would think that the /D command might do what you need. I would throw in a /M too.
xcopy \\hqcp-appsvr01\Files\ApplicationDocuments\AppDocs\49\PHOTO\*.* \\hqcp-appsvr02\Files\ApplicationDocuments\AppDocs\49\PHOTO\*.* /y /d /m
There is no reason to use pushd. The following modification of the for loop may be enough.
for /f %%a in ('dir "%source%" /b /a-d /o-d') do call :PROCESS "%%a"
Related
I have a directory with .txt files. I need to loop over them and perform two tasks: pass the file into Oracle's stored proc and move it into archive directory. For some reason second task does not work for me. What am I missing?
REM -----------------------------------------
REM first step changes file extension to .632
REM -----------------------------------------
for /f %%I in ('dir /b \\db01\load\*.txt') do (sqlplus.exe usr/pwd#DB #\\db01\sql\load.sql %%I)
for /f %%I in ('dir /b \\db01\load\*.632') do (move \\file01\archive)
Here's a couple of untested examples:
#Echo Off
PushD "\\db01\load" 2>Nul || Exit /B
For %%A In (*.txt *.632) Do (
If /I "%%~xA"==".txt" sqlplus usr/pwd#DB #"..\sql\load.sql" "%%A"
If /I "%%~xA"==".632" If Exist "..\..\file01\archive\" Move /Y "%%A" "..\..\file01\archive" >Nul
)
PopD
#Echo Off
PushD "\\db01\load" 2>Nul || Exit /B
For %%A In (*.txt) Do sqlplus usr/pwd#DB #"..\sql\load.sql" "%%A"
If Exist "*.632" Move /Y "*.632" "..\..\file01\archive" >Nul
PopD
Additionally, depending upon your sqlplus command requirements, you may wish to look at the Start command usage information, start /?
I have the following batch file:
#echo off
for /f "delims=" %%F in (
'dir /b /a-d [*]*'
) do for /f "tokens=1* delims=]" %%A in (
"%%F"
) do for /f "tokens=*" %%C in ("%%B") do ren "%%F" "%%C"
I want launch it in the root directory and have it go through all directories and subdirectories performing the actions.
I tried adding /D and /r to the 'for' lines, but it doesn't appear to be working.
Do I need add something like...
for /D /r do
under the #echo off ?
Use either dir or for to get all the files, don't mix it up.
When using dir /S for recursive enumeration, regard that full paths are output rather than pure file names only.
This should do it:
#echo off
for /f "delims=" %%F in (
'dir /s /b /a-d [*]*'
) do for /f "tokens=2* delims=]" %%B in (
"%%~nxF"
) do for /f "tokens=*" %%C in ("%%B") do ren "%%~F" "%%C"
So I just changed the following in your original code:
added /s to dir (returns full paths then);
improved second for options (you never used the first token %%A, so why extract it then?);
replaced set %%F of second for by %%~nxF to just parse the file name (type for /? for details concerning substitution modifiers such as ~n, ~x);
replaced source argument "%%F" of ren command by "%%~F" to not fall into double-double-quote problems (the ~ modifier removes potential double-quotes);
You are using "dir" for the enumeration of files, so add "/s" to the DIR command.
I might refactor what you have like this to make it easier to manage.
This also does recursion.
call :TOP .
goto :EOF
:TOP
setlocal
cd "%~f1"
for /f "delims=" %%F in ('dir /b /a-d [*]*') do call :SubRoutine "%%F"
for /D %%x in (*) do call :TOP "%%x" || (echo FAILED2 "%%x" && exit /b 2)
goto :EOF
:SubRoutine
for /f "tokens=1* delims=]" %%A in ("%~1") do call :SubRoutine2 "%~1" "%%A" "%%B"
goto :EOF
:SubRoutine2
for /f "tokens=*" %%C in ("%~3") do ren "%~1" "%%C"
goto :EOF
i am trying to make a script to remove empty folders and delete files a number of days old. depending on what the txt file delimiters are set to. I have came up with this so far:
::Batch
SET CDID=%~dp0
SET TEST=TRUE
IF %TEST%==TRUE (
SET COMND1=ECHO
SET COMND2=ECHO
) ELSE (
SET COMND1=DEL
SET COMND2=RD
)
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\LOG.TXT
FOR /F "usebackq delims=| tokens=1,2" %%x IN (%CDID%PATH.txt) DO (
CALL :DEL_FOLDERS "%%x" %%y
CALL :DEL_FILES "%%x" %%y
)
GOTO :EOF
:DEL_FILES
FORFILES /p %1 /s /m *.* /d %2 /c "cmd /c %COMND1% #file"
GOTO :EOF
:DEL_FOLDERS
FOR /f "delims=" %%i in ('dir %%1 /s /b /ad ^| sort /r') do %COMND2% "%%i"
GOTO :EOF
::PATH.txt
C:\Temp\BLANK|10
C:\Temp\New folder|30
when i run the script #file will not populate and %%i will not populate, i am not sure what i am doing wrong. Help?
You made a couple of very small errors. In DEL_FOLDERS you used %%1 which meant that the argument was not expanded (you only needed one % here). You also did not handle the case where there are no files that match or the directories are empty. In the FORFILES command you put /m *.*; although the documentation says this is the default, the documentation is incorrect. Missing out the /m matches all files (the default) but by saying /m *.* you only match files with a dot!
My corrected version is:
::Batch
SET CDID=%~dp0
SET TEST=TRUE
IF %TEST%==TRUE (
SET COMND1=ECHO
SET COMND2=ECHO
) ELSE (
SET COMND1=DEL
SET COMND2=RD
)
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\LOG.TXT
FOR /F "usebackq delims=| tokens=1,2" %%x IN (%CDID%PATH.txt) DO (
CALL :DEL_FOLDERS "%%x" %%y
CALL :DEL_FILES "%%x" %%y
)
GOTO :EOF
:DEL_FILES
FORFILES /p %1 /s /d %2 /c "cmd /c %COMND1% #file" 2> nul
GOTO :EOF
:DEL_FOLDERS
FOR /f "delims=" %%i in ('dir "%~1" /s /b /ad 2^>nul ^| sort /r') do %COMND2% "%%i"
GOTO :EOF
::PATH.txt
C:\Temp\BLANK|10
C:\Temp\New folder|30
I have the following batch file script code to run. When I run it, it executes successfully, but does not close the cmd window automatically. What should I add to have it close the window automatically?
::Copy Files Made Or Modified Today
#echo off
setlocal
set source=c:\src
set dest=c:\dest
pushd "%source%"
set t=%date:~4%
for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
goto :eof
popd
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" copy %1 "%dest%"
goto :eof
Test this if you are clicking on the batch file.
#echo off
::Copy Files Made Or Modified Today
setlocal
set source=c:\src
set dest=c:\dest
pushd "%source%"
set t=%date:~4%
for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
cls
goto :eof
popd
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" copy %1 "%dest%"
goto :eof
how to check folder size in windows through command prompt
say for example in C:\Windows there are many files and folders.
How to get the size of these files and folders
Is there any command similar to du -sg * in unix?
I have tried dir which will give the file not folders
#ECHO OFF
SETLOCAL
FOR /f "tokens=1,2,3" %%a IN ('dir /s') DO (
IF "%%b"=="File(s)" SET $files=%%a&SET $bytes=%%c
IF "%%b"=="Dir(s)" SET $dirs=%%a&SET $free=%%c
)
SET $
GOTO :EOF
This should set some variables of interest.
You should be able to insert a pusd/popd bracket
PUSHD someotherdirectory
for /f ....
...
)
POPD
...
to read the characteristics of someotherdirectory if you prefer.
Size of Windows folders - sorted and listed by MB - high to low
#echo off
pushd "c:\windows"
for /f "delims=" %%a in (' dir /ad /b ') do call :size "%%~fa"
sort /r < "%temp%\dirsize.tmp" |more
del "%temp%\dirsize.tmp"
popd
pause
goto :eof
:size
for /f "tokens=3" %%b in ('dir /s "%~1" 2^>nul ^|find " File(s) "') do set "n=%%b"
set "n=%n:,=%"
>"%temp%\VBS.vbs" echo Set fso = CreateObject("Scripting.FileSystemObject"^) : Wscript.echo int((%n%/1024/1024))
for /f "delims=" %%z in ('cscript /nologo "%temp%\VBS.vbs"') do set "dirsize=%%z"
del "%temp%\VBS.vbs"
set dirsize= %dirsize%
set dirsize=%dirsize:~-15%
>>"%temp%\dirsize.tmp" echo %dirsize% "%~1"