I need to move files from C:\users\users name\documents\sound recordings\ directory to our network drive P:\transcription\users name\. I need the files to automatically get renamed if the file exist in the P:\transcription\users name directory. I am a novice and can only write simple batch files and the one I wrote to move the files will over write the files with the same name in the directory. Thanks
This may help.
It will display files to be moved with resulting filename. If it works ok for you , remove the line echo !src!%%~nxa - !dst!!file! and delete the rem keyword from rem move !src!%%a !dst!!file! >nul
#echo off
SetLocal EnableDelayedExpansion
set "wildcard=*.*" & rem files to search for
set "skipUsers=Guest Administrator" & rem users not to copy files
:: get users
set/a offset=0
for /F "skip=2 tokens=2 delims=," %%1 in ('wmic useraccount get name^,sid /format:csv') do (
set/a offset+=1, USER_MAX=offset
set "USER[!offset!]=%%1"
)
for /L %%i in (1,1,%USER_MAX%) do (
set/a skip=0, numFiles=0
set "currentUser=!USER[%%i]!"
set "src=C:\!currentUser!\documents\sound recordings\"
set "dst=P:\transcription\!currentUser!\"
rem get time stamp
for /f "tokens=2 delims==" %%A in ('wmic os get localdatetime /value') do set "Tm=%%A"
set "timeStamp=!Tm:~0,4!!Tm:~4,2!!Tm:~6,2!_!Tm:~8,2!!Tm:~10,2!!Tm:~12,2!"
for %%a in (%skipUsers%) do if /I "!currentUser!" equ "%%a" set/a skip=1
if !skip! neq 0 ( rem avoid copying blacklist users
echo !currentUser!: Skipping
) else (
<nul set/P="!currentUser!: Moving files"
pushd !src!
dir /ad !dst! >NUL 2>NUL || md !dst! & rem create destination folder if it doesn't exist
for %%a in (%wildcard%) do (
set "file=%%~nxa"
if exist "!dst!!file!" set "file=%%~na_!timeStamp!%%~xa" & rem rem if file exist, append a time stamp suffix to avoid overwriting
echo !src!%%~nxa - !dst!!file!
rem move !src!%%~nxa !dst!!file! >nul
set/a numFiles+=1
)
echo : !numfiles! files moved
popd
)
)
EndLocal
exit/B
Related
Want to make a batch file from a folder of 30,000 files from let's say C:\Users\NAME\Desktop, that will create a folder named "1", put 500 files in, no matter the name of the file, and then loop naming the next "2", until all 30,000 are in folders of 500 files each.
Thanks in advance for the help :)
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
PUSHD "%sourcedir%"
ECHO sd=!sourcedir!
FOR /f "tokens=1*delims=:" %%b IN (
'dir /b /a-d * png *.doc^|findstr /n "."'
) DO (
SET /a subdir=1+(%%b / 7^)
FOR /f "tokens=2delims==" %%e IN ('set subdir') DO (
ECHO MD "%destdir%\%%e" 2>NUL
ECHO MOVE "%%c" "%destdir%\%%e\"
)
)
popd
GOTO :EOF
Always verify against a test directory before applying to real data.
I used a limit of 7 for testing. Modify as you will
Required md and move commands are merely echoed for verification. remove the echoes to activate after testing.
Found this from Compo, sorry didn't reference it!
#Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
SetLocal EnableDelayedExpansion
Set "DirN=-1"
:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
Set "limit=2"
For %%A In (*.png *.doc) Do (
If Not Exist "%DirN%" MD "%DirN%"
If /I Not "%%~nxA"=="%~nx0" RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
Set/A "limit-=1"
If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
Timeout -1 1>Nul
I have many folders in a directory which I need to rename with a fixed base name and a progressive number starting from 1 to infinite.
Path of folders have space and base folder is D:\Programmi Installati.
Example of folders to rename:
log_1
log_2
log_04-01-2019 15-15-11,51
log_01-01-2019 8-22-14,19
log_27-12-2018 14-23-18,28
log_aaaa
log_bbbb
log_5
log_6
log_02-01-2019 6-21-17,34
log_03-01-2019 21-18-16,22
Example of wanted folder names:
log_1
log_2
log_3
log_4
log_5
log_6
log_7
log_8
log_9
log_10
log_11
log_12
The numbers of folder to rename can be large, but the structure is the same.
I tryed more batch file but all fail when there are some folders with the wanted name (example log_5 or log_1)
The order is not important it is important that all the folders starting with "log" are renamed with an incrental number.
Code already tryed without success
:: 1 code
#echo off
setlocal enabledelayedexpansion
set counter=
for /d %%a in ("D:\Programmi Installati\log_*") do (
set /a counter+=1
ren "%%~fa" "log_!counter!"
)
pause
:: 2 code
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Counter=1"
for /F "delims=" %%I in ('dir "D:\Programmi Installati\log*" /AD /B /ON 2^>nul') do ren "D:\Programmi Installati\%%I" "log_!Counter!" & set /A Counter+=1
endlocal
pause
:: 3 code
#ECHO OFF
#setlocal enabledelayedexpansion
Rem | Folder Path & CD To Location
Set "Folder=D:\Programmi Installati\"
CD %Folder%
Rem | Get Raw File Name
Set "Number=1"
for /F "tokens=*" %%A in ('dir "log*" /S /b /AD') do (
Rem | Rename Folder || Raw Name - %%~n1
rename "%%~nA" "log_!Number!"
Rem | Add One To Number
set /a "number+=1"
)
Goto :EOF
PAUSE
The codes over works only if there is no desired directory name in the directory otherwise do not rename folders.
This batch works differently, it
skips folders with the proper naming scheme (so they keep the number)
increments a counter and fills in possible gaps
:: Q:\Test\2019\01\11\SO_54149437.cmd
#Echo off
Pushd "D:\Programmi Installati\" || (Echo couldn't change dir&pause&goto :eof)
set Cnt=0
for /f "delims=" %%A in (
'dir /B /AD log_* ^| findstr /iV "^log_[0-9][0-9]*$" '
) Do Call :RenInc "%%A"
PopD
Goto :Eof
:RenInc
Set /A Cnt+=1
if Exist "log_%Cnt%" goto :RenInc
Ren "%~1" "log_%Cnt%"
The resulting names (there are only eleven, not twelve)
log_1
log_10
log_11
log_2
log_3
log_4
log_5
log_6
log_7
log_8
log_9
I want to fetch two sub string from my file name in order to create Folder String.
My file name is "SM-SM-ABC_ab12 cd34_AA 11_abc123.txt"
here "ab12 cd34" is 1st folder and "AA 11" is 2nd folder
I have written a code but After adding #Compo code I ma not able to move file to directory. I want to move multiple files to respective folders.
Can some one help whats wrong?
#Echo Off
set Path1= d:\A
:: SDate=DAYMONTHYEAR FORMAT of Systemdate
echo %Path1%
set SDate=%date:~7,2%%date:~4,2%%date:~10,4%
echo %SDate%
::Variable for folder path
Pushd %Path1%
for %%i in (*.*) do SET "FPath=%%~ni"
For /F "Tokens=2-3 Delims=_" %%A In ("%FPath%") Do (
Set "FoldOne=%%A"
Set "FoldTwo=%%B"
if not exist "%Path1%\%FoldOne%\%FoldOne%\%SDate%" (
mkdir "%Path1%\%FoldOne%\%FoldOne%\%SDate%" )
move %Path1%\* "%Path1%\%FoldOne%\%FoldTwo%\%SDate%\"
echo test %Path1%
echo test %FPath%
)
GoTo :EOF
Is this what you are trying to achieve?
#Echo Off
Set "FPath=SM-SM-ABC_ab12cd34_AA11_abc123.txt"
For /F "Tokens=2-3 Delims=_" %%A In ("%FPath%") Do (
Set "FoldOne=%%A"
Set "FoldTwo=%%B")
Echo(%%FoldOne%%=%FoldOne%
Echo(%%FoldTwo%%=%FoldTwo%
Timeout -1
GoTo :EOF
[Edit /]The following code may provide you with a solution for your updated requirements:
#Echo Off
Set "Path1=D:\A"
If /I Not "%CD%"=="%Path1%" Pushd "%Path1%" 2>Nul || Exit/B
For /F "EOL=L" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "SDate=%%B"
Set "SDate=%SDate:~6,2%%SDate:~4,2%%SDate:~,4%"
For %%A In ("*_*_*_*.*") Do Call :Sub "%%A"
Timeout -1
GoTo :EOF
:Sub
For /F "Tokens=2-3 Delims=_" %%A In (%1) Do If Not "%%A"=="" If Not "%%B"=="" (
If Not Exist "%%A\%%B\%SDate%\" MD "%%A\%%B\%SDate%"
Move %1 "%%A\%%B\%SDate%")
Scenario:
We have multiple releases of a product, and for each release, a folder is created in the main folder. A help file is modified in various releases. I have all the help file names listed in a text file.
I need a script to:
Take each file name from the filenames.txt file
Search for the file by that name in the entire directory (in all releases)
Find the latest file
Copy it to a specified folder
I took help from the various pieces of code I found on Stack Overflow, and combined them to get this code:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
echo.
FOR /F "usebackq delims=" %%a in ("filenames.txt") do (
SET "x=%%a"
ECHO '!x!'
SET FFPath=C:\SVN\nlbavwdocsvn\rep_doc_erpln\trunk\ERPLN
SET NewPath=C:\Lavanya\extracted
SET NewestDate=20160824
ECHO Recursively searching %FFPath%
FOR /F %%I in ('DIR %FFPath%\ !x! /a:-d /s /b') DO (
SET FullDate=%%~tI
ECHO %FFPath%
REM Set CurrDate to yyyymmdd format. Note: Will fail if regional settings changed.
SET CurrDate=!FullDate:~6,4!!FullDate:~0,2!!FullDate:~3,2!
If !CurrDate! gtr !NewestDate! (
SET NewestDate=!CurrDate!
SET NewestFile=%%~fI )
ECHO Copying %NewestFile% to %NewPath%
ECHO.
COPY /Y "%NewestFile%" "%NewPath%"
ECHO.
)
)
PAUSE
This code is not working. And I am unable to figure out the error.
Here is a script to search for the most recently modified file, using the wmic command to retrieve the last modification date/time in a locale-independent manner (e. g., 20160824115500.000000+060).
So for every file name read from the list file .\filenames.txt, the directory tree routed at directory C:\SVN\nlbavwdocsvn\rep_doc_erpln\trunk\ERPLN is searched for matching files recursively, and the respective modify date/time stamp is gathered. Due to its format, a simple greater-than (GTR) comparison can be done do determine whether or not it is a later point of time than a cached one; if the criterion is fulfilled, the cache is updated accordingly.
The upper-case REM and ECHO commands constitute placeholders only for the real action to be performed on the files. Extend the script there as you like. Variable !LASTFILE! holds the full path to each encountered file.
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "LOCATION=C:\SVN\nlbavwdocsvn\rep_doc_erpln\trunk\ERPLN"
set "FILELIST=.\filenames.txt"
set "WMICPROP=LastModified" & rem // {CreationDate | LastAccessed | LastModified}
pushd "%LOCATION%" || exit /B 1
for /F "usebackq eol=| delims=" %%L in ("%FILELIST%") do (
set "LASTFILE="
set "LASTFAGE=00000000000000.000000+000"
for /F "eol=| delims=" %%F in ('dir /B /S /A:-D "%%~L"') do (
set "FILE=%%F"
setlocal EnableDelayedExpansion
set "FILE=!FILE:\=\\!"
for /F "tokens=2 delims==" %%J in ('
2^> nul wmic DataFile WHERE ^(Name^="!FILE!"^) GET %WMICPROP% /VALUE ^|^| ^
2^> nul wmic DataFile WHERE Name^="!FILE!" GET %WMICPROP% /VALUE
') do for /F %%I in ("%%J") do (
endlocal
set "FAGE=%%I"
setlocal EnableDelayedExpansion
if !FAGE! GTR !LASTFAGE! (
endlocal
set "LASTFILE=%%F"
set "LASTFAGE=%%I"
setlocal EnableDelayedExpansion
)
)
endlocal
)
if defined LASTFILE (
setlocal EnableDelayedExpansion
REM Do whatever you want with the file here...
ECHO Newest file: "!LASTFILE!"
endlocal
)
)
popd
endlocal
exit /B
I have this batch to check export a list of programs installed and their uninstallers:
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
MKDIR "%userprofile%\desktop\CloudUninstall"
CD "%userprofile%\desktop\CloudUninstall"
regedit /e regexport.txt "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"
find "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\" regexport.txt >> reg1.txt
set /a var=2
GOTO LOOPD
:LOOPD
FOR /F "skip=%var% tokens=* delims==" %%A in (reg1.txt) do set trans=%%A & GOTO WALKIT
PAUSE
:WALKIT
REM This transits the variable from the for /f loop into the current function
set current=%trans%
REM This then takes the REGEDIT string formatting and reformats it to standard text for new function
regedit /e %var%.txt "%current:~1,-2%"
find "DisplayName" %var%.txt >> %var%_a.txt
find "UninstallString" %var%.txt >> %var%_a.txt
set /a var=var+1
GOTO LOOPD
What I need is a way to compare a text file with names of programs against this set of files. Then, if the name from the text file appears in that list of files, export the uninstaller into a separate text file.
#echo off
rem Configure environment
setlocal enableextensions disabledelayedexpansion
rem Configure our "blacklist" file
set "blackList=%temp%\blacklist.txt"
rem To test, generate the blacklist file
(
echo Adobe Flash Player
echo Dell Touchpad
echo Nokia Suite
) > "%blacklist%"
echo.
type "%blacklist%"
echo.
rem From where to retrieve the information
set "key=HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"
rem Variables used for temporary storage of values
set "displayName="
set "uninstallString="
rem Query the registry for the desired values and store values in variables. Each time a Uninstall key is found
rem output data. The complete list is being sent to a temporary file
(
for /f "tokens=1,2,*" %%a in ('reg query "%key%" /s ^| findstr /i /l /c:"%key%" /c:"DisplayName" /c:"UninstallString"') do (
if /i "%%a"=="DisplayName" (
set "displayName=%%c"
) else if /i "%%a"=="UninstallString" (
set "uninstallString=%%c"
) else (
if defined displayName if defined uninstallString (
setlocal enabledelayedexpansion
if not "!uninstallString:~0,1!!uninstallString:~0,1!"=="""" (
set "uninstallString="!uninstallString!"
set "uninstallString=!uninstallString:.exe=.exe" !"
)
echo(!displayName!^|!uninstallString!
endlocal
)
set "displayName="
set "uninstallString="
)
)
if defined displayName if defined uninstallString (
setlocal enabledelayedexpansion
echo(!displayName!=!uninstallString!
endlocal
)
) > "%temp%\ProgramList"
rem Compare the generated program list against black list
echo.
type "%temp%\ProgramList" | findstr /i /r /g:"%blacklist%"
echo.
rem Extract uninstallstring from list to another file
(for /f "tokens=1,* delims=^|" %%u in (
'findstr /i /r /g:"%blacklist%" ^< "%temp%\ProgramList"'
) do (
set "c=%%v"
echo(%%v
))>"%temp%\UninstallList"
echo.
type "%temp%\UninstallList"
echo.
endlocal
exit /b