xcopy batch file pst backup - file

I'm trying to build a batch file script, which will copy the outlook.pst file, from the user's My Documents folder and move it to the server, which I've mapped as a drive, "B:\"
This is the code I have so far:
#echo on
xcopy "c:\Documents and Settings\%username%\My Documents\outlook.pst" "B:\PST\%username%\" -c -d -i -y
exit
The script is designed for Windows XP.
However, when I run this code on the client machine, it doesn't copy the .pst file across, it just runs the command over and over again, until I Ctrl + C it...
Thanks

If I recall correctly, the default location for PST files is in %localappdata%\Microsoft\Outlook\. And if the user has PST files in multiple locations, it's possible that he has more than one with the same name, just in different folders. Good times.
If it's possible that your users might have PST files in locations other than My Documents, I recommend modifying your script with just a couple of very minor changes.
#echo off
setlocal enabledelayedexpansion
for /r "%userprofile%" %%I in (*.pst) do (
rem avoid overwriting in case "outlook.pst" exists in two locations, for instance
if exist "B:\PST\%username%\%%~nxI" (
set cnt=000
rem get count of files matching b:\pst\username\filename*.pst
for /f %%a in ('dir "B:\PST\%username%\%%~nI*%%~xI" 2^>NUL ^| find " File(s) "') do (
set "cnt=!cnt!%%a"
)
rem file of the same name will become filename001.pst, filename002.pst, etc.
set "dest=%%~nI!cnt:~-3!%%~xI"
rem otherwise leave the filename alone
) else set "dest=%%~nxI"
set /P "=Copying !dest!... "<NUL
copy "%%~fI" "B:\PST\%username%\!dest!"
echo Done.
)

I wished to copy outlook.pst from its Microsoft default location to a memory stick using a batch file in Windows XP
I found that xcopy failed to copy Outlook.pst to the memory stick or to My Documents
I now xcopy it in 2 steps, firstly to C:\ then to the Memory Stick
It has worked reliably

Related

Compare two folders' contents instead of two files contents' through batch file

I'm writing backup batch file for a particular folder that is located in a shared network folder, to my personal Windows machine.
I want to keep track of changes made on this network folder, so I keep a lot of backup folders which have a datestamp+timestamp name like 20181224145231 which is the backup folder created on the 24th of December (12), 2018 at 14h52min31sec.
All of my backup folders of datestamp+timestamp are located in a separate folder.
To do this I came up with a script that grabs date and time from the system and checks if a particular file in the original folder is different than the one located in the last backup folder using fc and a for loop to grab the last backup folder created in the past.
Things have grown and I need to compare the content of the whole folder (with subfolders) and not just a file. And that's where I've hit a wall.
I've looked into comp and fc, but can't seem to find a way. Robocopy syncs folders, but I want to create a new folder each time changes have occured. One thing I'm thinking about is creating a comparison file in both folders like a 7-zip file and running fc on both of these, but it seems rather extreme.
So summing up my question is:
How to check if the most recent backup has the same files as with the network shared folder without third party-tools through batch file?
According to your requirements, specified in comments, you can try:
#echo off
rem Set variables for size count:
set total_size_backup=0
set total_size_origin=0
rem Find the most recent BACKUP folder:
for /F "delims=" %%A IN ('dir /b /AD /OD') do set "folder_to_search=%%~fA"
rem Find the size of all files inside the backup folder:
for /R "%folder_to_search%" %%B IN (*.*) do (
set /a "total_size_backup+=%%~zB"
)
rem Find the size of the original folder:
for /R "full_path_to_folder_with_original_files" %%C IN (*.*) do (
set /a "total_size_origin+=%%~zC"
)
rem Compare the two sizes from these two folders. If they are NOT the same include your code there.
if %total_size_backup% EQU %total_size_origin% (
echo Well Done! Your newest backup files and your original files are up-to-date!
pause>nul
exit /b 0
) else (
echo Ooops! Your newest backup files and your original files are out-of-date! Never worry! Running backup now, please wait...
start /min /wait your_backup_file.bat
echo Updated files successfully!
exit /b %errorlevel%
)

Rename files with current date stamp (bat files)

I have bat file to run sql procedure which is creating 2 files:
TEST_SCHEMA.dmp
TEST_SCHEMA.log
Then I used xcopy to copy files. After copy I want to change the file name to:
TEST_SCHEMA.dmp -> TEST_CURRENT_DATE.dmp
TEST_SCHEMA.log -> TEST_CURRENT_DATE.log
where Current_Date = YYYYMMDDHHMM
This is what I had so far:
set OWNER=myOwner
set FILE_NAME=TEST_SCHEMA
set NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1
sqlplus %OWNER%/%OWNER%#host:1521/SID #run_test.sql %OWNER% %FILE_NAME%
xcopy "\\host\c$\abc\def\test\%FILE_NAME%.*" /C
set "_year=%MyDate:~0,4%"
set "_month=%MyDate:~4,2%"
set "_day=%MyDate:~6,2%"
ren FILE_NAME.dmp "TEST (%_year%%_month%%_day%).txt"
ren FILE_NAME.log "TEST (%_year%%_month%%_day%).txt"
pause 0
Result should be like:
TEST_201806131006.dmp
TEST_201806131006.log
But I don't know how to correctly use rename and put date to file name.
Using WMIC to create the DateTime stamp:
For /F %%A In ('WMIC OS Get LocalDateTime') Do If Not "%%~xA"=="" Set "ds=%%~nA"
Ren "TEST_*.*" "TEST_%ds:~,-2%.*"
Create the file including the date stamp.
set OWNER=myOwner
for /f %%A in ('
powershell -nop -c "get-date -f yyyyMMdd"
') do SET "FILE_NAME=TEST_SCHEMA_%%A"
set NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1
sqlplus %OWNER%/%OWNER%#host:1521/SID #run_test.sql %OWNER% %FILE_NAME%
xcopy "\\host\c$\abc\def\test\%FILE_NAME%.*" /C
The above answers seem a little complicated to me. I have some files that I am constantly updating as I develop a program. In case I break something and don't realize immediatly I needed a way to backup several times a day. This is what I put in my batch program (modified for your file name) which I then control via Windows Task Scheduler. (I actually xcopy the folder I'm working on and then rename it with the date/time adjustment.)
ren TEST_SCHEMA.dmp "TEST_%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%.dmp"
My thanks to Mofi for providing his answer to another user here...
What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?

Batch: Preserve (creation-)date on copying files to another (flatten) folder structure, incl. renaming files to avoid doublettes

this is my first question, so I apologize beforehand if I write not as you are used to...
fact:
I've a deep folder structure with tons of files (images,videos and so on) so I want to copy that files to a flat structure for a better overview.
I want to keep (at least) the original date attributes: creation-date and last-modified date.
Problem 1) there are files with same name 00001.jpg in different folders which I want to have in same folder, so I want to add creation date/time to filename on copy process.
00001.jpg becomes 2015-11-17_11-23-35_00001.jpg
So far so good. Or not good...
Copy and XCopy doesn't give me an option to do that, without loosing at least the creation date information (I didn`t find a solution with both).
Now I try to copy the files (file by file) with robocopy to new folder and use ren on the copied file to "pre-set" the date/time information before the filename.
Here is a simple test.bat example:
setlocal enableextensions enabledelayedexpansion
robocopy . ./zzz original.txt /copy:DATSO
pause
rem :: formatted creation date of original file will be here, in real life
set "myDate=2015-11-17-_11-23-35"
rem rename "./zzz/original.txt" "!myDate!_renamed.txt" ::doesnt work: why? relative path??
rem :: this will do what I want - original creation date is kept on copy file
FOR %%A IN (zzz/original.txt) DO REN "%%~fA" "!myDate!_%%~nxA"
[possibly] Problem2) Is there a better way to do this, or could I run into thread problems (asynchronous execution). Could it be, that I try to rename a file before the robocopy has finished the copy process (e.g. for large files)?
Sorry I'm a totally batch newbie (also as poster in SO ;).
ThanX in advance for each tip and also for critics on my solution approach. Maybe I have the horse-blinkers on my head and dont see the easy solution?!
[edit: formatting of post]
[edit: content of post -> date/time in front of filename for better sorting]
It is possible to use command DIR to get recursive listed all files in the specified folder and its subfolders with creation date/time.
The format of the date/time depends on Windows Region and Language settings.
Example output for F:\Temp\Test on my machine with English Windows 7 and region is configured to Germany on running the command line dir F:\Temp\Test\* /A-D /S /TC:
Volume in drive F is TEMP
Volume Serial Number is 1911-26A4
Directory of F:\Temp\Test
25.09.2017 17:26 465.950 SimpleFile.ccl
1 File(s) 465.950 bytes
Directory of F:\Temp\Test\Test Folder
25.09.2017 17:26 360.546 Test File.tmp
1 File(s) 360.546 bytes
Total Files Listed:
2 File(s) 826.496 bytes
0 Dir(s) 58.279.460.864 bytes free
This output is filtered with findstr /R /C:"^ Directory of" /C:"^[0123][0123456789]" to get only lines starting with  Directory of (note the space at begin) or with a number in range 00 to 39.
Directory of F:\Temp\Test
25.09.2017 17:26 465.950 SimpleFile.ccl
Directory of F:\Temp\Test\Test Folder
25.09.2017 17:26 360.546 Test File.tmp
And this filtered output is processed by FOR loop and the commands executed by FOR.
#echo off
for /F "tokens=1-2*" %%A in ('dir "F:\Temp\Test\*" /A-D /S /TC ^| %SystemRoot%\System32\findstr.exe /R /C:"^ Directory of" /C:"^[0123][0123456789]" 2^>nul') do (
if "%%A %%B" == "Directory of" (
set "FilePath=%%C"
) else (
set "CreationDate=%%A"
set "CreationTime=%%B"
for /F "tokens=1*" %%I in ("%%C") do set "FileName=%%J"
call :RenameFile
)
)
goto :EOF
:RenameFile
set "NewName=%CreationDate:~-4%-%CreationDate:~3,2%-%CreationDate:~0,2%_%CreationTime:~0,2%-%CreationTime:~3,2%_%FileName%"
ren "%FilePath%\%FileName%" "%NewName%"
goto :EOF
It would be advisable to put command echo before command ren in last but one line to first verify the expected new file names.
ren "F:\Temp\Test\SimpleFile.ccl" "2017-09-25_17-26_SimpleFile.ccl"
ren "F:\Temp\Test\Test Folder\Test File.tmp" "2017-09-25_17-26_Test File.tmp"
Note: The batch file must be in a folder not processed by this batch file as otherwise the batch file itself would be renamed while running which breaks the processing of the batch file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
dir /?
echo /?
for /?
goto /?
if /?
ren /?
set /?
By the way: WinRAR can add files into a RAR archive with creation and last access time in addition to last modification time and extract the files to a different directory with restoring also creation and last access time using SetFileTime function of Windows kernel.
currently I use Locale independent date. I use tokens from that for currrent date/time.
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
rem :: format it to YYYY-MM-DD_hh-mm-ss
set myDateTime=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2%_%datetime:~8,2%-%datetime:~10,2%-%datetime:~12,2%
Thats not the problem.
To clarify:
The listing is also not the problem.
I loop throw all related files without a prob (except batch files and output dir and its sub tree ;).
My problem is:
I call robocopy for each file, then I rename the file to save the original creation date. My fear is that it makes problems (Multi-Threading?) for large files and for the number of calls (many thousend times)
Is batch execution really serialized? Is the process waiting for robocopy, that it has finished, before I try to rename file. Could I run into dead-locks for vry large files? I'll test it with some fake gigabyte-files.
Your suggestion, to use winrar sounds interesting.
If I could add all that files to a big archive (with structure) and at the end extract it to target dir... I'll try it ;)
If it doesn't work I will program it in java!
There I know what to do, thats my playground :D
I thought it would be easy to write a simple batch file, to do this for me, but it seems it's not as easy as I thought!
ThanX anyway

CMD Batch script to sort files

I have an application which saves files to a FTP folder that I sync to my PC which contains multiple JPG and MP4 files, named in the following format:
ARC20170510151547.jpg
ARC20170510151549.mp4
What I'm trying to do is:
Copy the files from my FTP to my PC
Sort the files into folders based on the day they were created
Delete files from the FTP older than 14 days
Delete files from the PC older than 1 month
Using WinSCP to connect to FTP with the following code, I am able to download all the files to my local drive:
"c:\program files (x86)\winscp\winscp.com" /ini=nul /command ^
"open ftp://[username]:[password]#[ipaddress]/" ^
"synchronize local f:\[localpath]\ /[remotepath]/ " ^
"exit"
Then I need to sort the files. Here is where I am stuck. I'm think I know the commands but I am unsure how to use the 'tokens' and 'delims' to get it to work how I want.
#echo
for %%a in (*.*) do (
echo processing "%%a"
for /f "tokens=1 delims=" %%a in ("%%~nxa") do (
md "%%b-%%c" 2>nul
move "%%a" "%%b-%%c" >nul
)
)
pause
As I know the filename format isn't going to change, one thing I have considered is adding some special characters to the filename, maybe using the 'ren' command. I could then use those special characters as search delims but, again, I'm struggling how to best proceed.
Removing local files older than 30 days is easy using the following script
forfiles -p "f:\[localpath]" -s -m *.* -d <number of days> -c "cmd /c del #path"
However, the WinSCP 'RM' command I am using doesn't appear to be working. It returns an error "no file matching '*<14D' found"
"rm /[filepath]/*<14D" ^
Any help, advice and guidance would be very gratefully received!
Since there is no delimiter between the date elements you need substrings,
substrings do only work with normal variables and in a (code block) you need delayed expansion
It's unclear if you want folders with year-month or month-day, select the proper part in the batch and comment/uncomment the Rem:
With extensions enabled md can create the structure YY-MM\DDin one step.
So you can move directly to that folder.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for %%a in (ARC*.*) do (
echo processing "%%a"
Set File=%%~nA
Set YY=!File:~3,4!
Set MM=!File:~7,2!
Set DD=!File:~9,2!
Rem YY-MM\DD
md "!YY!-!MM!\!DD!" 2>nul
move "%%a" "!YY!-!MM!\!DD!" >nul
)
pause
#LotPings! Thank you for the script. This does almost what I want it to do. I have modified the script as below which moves the files into folders based on the Day.
So, each day, maybe 100 - 200 files are generated. So I do not mind having a folder for each day. Once the files are moved into their respective "Day" folder, what I'd then like to do is create a SubFolder "!YY!-!MM!" and then move the "!DD!" Folders into the "!YY!-!MM!" folder.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for %%a in (ARC*.*) do (
echo processing "%%a"
Set File=%%~a
Set YY=!File:~3,4!
Set MM=!File:~7,2!
Set DD=!File:~9,2!
md "!DD!" 2>nul
md "!YY!-!MM!" 2>nul
move "%%a" "!DD!" >nul
)
pause

Windows batch move (or copy) PDF file by name to corresponding folder

I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.

Resources