Best way to compare some text files - powershell

Here is the simplified version of the situation I'm dealing with:
Folder Files1 containing a.txt, b.txt, c.txt
Folder Files2 containing a.txt, b.txt, c.txt
I want to find the best way to compare these files. All I need to know is which files are different. For example I need to know a.txt is different from a.txt in Files2 folder (I don't need to know what is different inside them).
Right now I load both in Notepad++ and use the compare function, but this is a pain.
I tried fc, but I'm getting cryptic output.

You could use Robocopy for that
robocopy c:\Temp\different\folder1 c:\temp\different\folder2 /e /l /ns /njs /njh /ndl /fp /log:c:\temp\whatIsDifferent.txt
Newer c:\Temp\different\folder1\b.txt
New File c:\Temp\different\folder1\d.txt
the key parameter is /L which allows you to "compare" instead of actually copying.
From Robocopy /? help
::
:: Logging Options :
::
/L :: List only - don't copy, timestamp or delete any files.

Do a dry-run (/l) with robocopy:
robocopy C:\files1 C:\files2 /njh /njs /ndl /l
/l Specifies that files are to be listed only (and not copied, deleted, or time stamped).

Rsync can be used to do this.
rsync -rvnc —delete delme/ delme2/
This will show you which files differ in the 2 directories.
See http://www.techrepublic.com/blog/linux-and-open-source/how-to-compare-the-content-of-two-or-more-directories-automatically/ for me details.

Related

How to use robocopy for selective file copying

I am using omake for my project build
As part of my delivery build I need to copy some delivery related files into a folder
How I can use robocopy execution where I can only copy the interested files(which are selected by running a for loop on a list I have)
Into the folder.
List_paths = filepath1
Filepath2
.
FilepathN
For file in list_paths
%exec robocopy $(file,A) $(destin)
End for
By doing that robocopy always complains me that there is no such file by adding \ at the end of eCh file and for destin folder too.
I Understand that robocopy just works fine for copying files from one folder to other but I have condition to copy only selective files from folders (for which I have the list of paths)
Please help on this.
If you have a textfile with complete patnames (i.e list.txt), with content:
d:\temp\a.sql
d:\temp\b.sql
d:\temp\c.sql
and a batchfile roboDo.bat like this:
#echo off
SET dest=d:\temp\temp\
FOR /f "useback tokens=1" %%f IN (`type list.txt`) DO (
ECHO copying %%~nxf from %%~dpf to %dest%
ROBOCOPY /NP /NJH /NJS %%~dpf %dest% %%~nxf >NUL
)
running the batchfile wil output this:
D:\TEMP>robodo
copying a.sql from d:\TEMP\ to d:\temp\temp\
copying b.sql from d:\TEMP\ to d:\temp\temp\
copying c.sql from d:\TEMP\ to d:\temp\temp\

CMD dir List video files - Not Found error

I crafted some command line code (function? I don't know the technical term, my apologies) that is meant to look in a network folder (and all it's subfolders) and only list the file path for those files that are a particular type. I tried to test it on a folder with less information in it first, however I got the error File Not Found. This confused me because I looked in the folder--there were definitely .VOB and .mp4 files in the specified folder.
When I run the code included below, I don't get the error, BUT I'm getting a bunch of files I don't want (.pdf .xml .jpeg--etc.) I think because they're system files such as the .jpeg files for the .avi thumbnails.
dir "\\nas-rb4b\projectx2" /s /b *.avi *.mov *.mp4 *.wmv *.mpg *.lnk *.ldb *.rar *.mpeg *.m4v *.vob *.zip>Projx2RERUN.txt
Can anyone help me understand why I got the File Not Found error when I tried to run the exact same code but with a different more specific folder?
I also don't know how to modify the code/function to exclude files that are not the listed file types and are not system folders. Any help is, as always, much appreciated!
Read the newest dir command reference in Windows Commands:
Syntax:
dir [<Drive>:][<Path>][<FileName>] [...] [/p] [/q] [/w] [/d] [/a[[:]<Attributes>]][/o[[:]<SortOrder>]] [/t[[:]<TimeField>]] [/s] [/b] [/l] [/n] [/x] [/c] [/4]
Remarks:
To use multiple FileName parameters, separate each file name with a
space, comma, or semicolon.
The command in question dir "\\nas-rb4b\projectx2" /s /b *.avi *.mov *.mp4
(truncated) says and performs the following:
dir "\\nas-rb4b\projectx2" /s /b, (i.e. all files in "\\nas-rb4b\projectx2"), then
dir /s /b *.avi (i.e. all .avi files in the current directory), then
dir /s /b *.mov (i.e. all .mov files in the current directory), then
dir /s /b *.mp4 (i.e. all .mp4 files in the current directory), …
Solution (read pushd and popd reference as well):
pushd "\\nas-rb4b\projectx2"
dir /s /b *.avi *.mov *.mp4 *.wmv *.mpg *.lnk *.ldb *.rar *.mpeg *.m4v *.vob *.zip>Projx2RERUN.txt
popd
Following some advice from Ken White along with a combination of the answers given here, here, and information from here I made the expression below which eventually worked for me.
dir /b /s \\nas-rb4b\projectx2\*.avi \\nas-rb4b\projectx2\*.mov \\nas-rb4b\projectx2\*.mp4 \\nas-rb4b\projectx2\*.wmv \\nas-rb4b\projectx2\*.mpg \\nas-rb4b\projectx2\*.rar \\nas-rb4b\projectx2\*.mpeg \\nas-rb4b\projectx2\*.m4v \\nas-rb4b\projectx2\*.vob \\nas-rb4b\projectx2\*.zip
It is not pretty but it allowed me to search for multiple file types using only one expression and have their paths all sent to one txt file rather than searching each file extension individually.
So far it is working as intended.

Use Robocopy to copy only changed files?

I'm trying to find an easy way of deploying only changed files to the webserver for deployment purpose. In times past I've used MSBuild, which could be told to only copy files that were newer than the ones on the target, but I'm in a hurry and don't want to try to figure out the newer version of MSBuild.
Can I use ROBOCOPY for this? There is a list of options for exclusion, which is:
/XC :: eXclude Changed files.
/XN :: eXclude Newer files.
/XO :: eXclude Older files.
/XX :: eXclude eXtra files and directories.
/XL :: eXclude Lonely files and directories.
What exactly does it mean to exclude? Exclude copying, or exclude overwriting? For example, if I wrote:
ROBOCOPY C:\SourceFolder\ABC.dll D:\DestinationFolder /XO
would this copy only newer files, not files of the same age?
Or is there a better tool to do this?
To answer all your questions:
Can I use ROBOCOPY for this?
Yes, RC should fit your requirements (simplicity, only copy what needed)
What exactly does it mean to exclude?
It will exclude copying - RC calls it skipping
Would the /XO option copy only newer files, not files of the same age?
Yes, RC will only copy newer files. Files of the same age will be skipped.
(the correct command would be robocopy C:\SourceFolder D:\DestinationFolder ABC.dll /XO)
Maybe in your case using the /MIR option could be useful. In general RC is rather targeted at directories and directory trees than single files.
You can use robocopy to copy files with an archive flag and reset the attribute. Use /M command line, this is my backup script with few extra tricks.
This script needs NirCmd tool to keep mouse moving so that my machine won't fall into sleep. Script is using a lockfile to tell when backup script is completed and mousemove.bat script is closed. You may leave this part out.
Another is 7-Zip tool for splitting virtualbox files smaller than 4GB files, my destination folder is still FAT32 so this is mandatory. I should use NTFS disk but haven't converted backup disks yet.
backup-robocopy.bat
#REM https://technet.microsoft.com/en-us/library/cc733145.aspx
#REM http://www.skonet.com/articles_archive/robocopy_job_template.aspx
set basedir=%~dp0
del /Q %basedir%backup-robocopy-log.txt
set dt=%date%_%time:~0,8%
echo "%dt% robocopy started" > %basedir%backup-robocopy-lock.txt
start "Keep system awake" /MIN /LOW cmd.exe /C %basedir%backup-robocopy-movemouse.bat
set dest=E:\backup
call :BACKUP "Program Files\MariaDB 5.5\data"
call :BACKUP "projects"
call :BACKUP "Users\Myname"
:SPLIT
#REM Split +4GB file to multiple files to support FAT32 destination disk,
#REM splitted files must be stored outside of the robocopy destination folder.
set srcfile=C:\Users\Myname\VirtualBox VMs\Ubuntu\Ubuntu.vdi
set dstfile=%dest%\Users\Myname\VirtualBox VMs\Ubuntu\Ubuntu.vdi
set dstfile2=%dest%\non-robocopy\Users\Myname\VirtualBox VMs\Ubuntu\Ubuntu.vdi
IF NOT EXIST "%dstfile%" (
IF NOT EXIST "%dstfile2%.7z.001" attrib +A "%srcfile%"
dir /b /aa "%srcfile%" && (
del /Q "%dstfile2%.7z.*"
c:\apps\commands\7za.exe -mx0 -v4000m u "%dstfile2%.7z" "%srcfile%"
attrib -A "%srcfile%"
#set dt=%date%_%time:~0,8%
#echo %dt% Splitted %srcfile% >> %basedir%backup-robocopy-log.txt
)
)
del /Q %basedir%backup-robocopy-lock.txt
GOTO :END
:BACKUP
TITLE Backup %~1
robocopy.exe "c:\%~1" "%dest%\%~1" /JOB:%basedir%backup-robocopy-job.rcj
GOTO :EOF
:END
#set dt=%date%_%time:~0,8%
#echo %dt% robocopy completed >> %basedir%backup-robocopy-log.txt
#echo %dt% robocopy completed
#pause
backup-robocopy-job.rcj
:: Robocopy Job Parameters
:: robocopy.exe "c:\projects" "E:\backup\projects" /JOB:backup-robocopy-job.rcj
:: Source Directory (this is given in command line)
::/SD:c:\examplefolder
:: Destination Directory (this is given in command line)
::/DD:E:\backup\examplefolder
:: Include files matching these names
/IF
*.*
/M :: copy only files with the Archive attribute and reset it.
/XJD :: eXclude Junction points for Directories.
:: Exclude Directories
/XD
C:\projects\bak
C:\projects\old
C:\project\tomcat\logs
C:\project\tomcat\work
C:\Users\Myname\.eclipse
C:\Users\Myname\.m2
C:\Users\Myname\.thumbnails
C:\Users\Myname\AppData
C:\Users\Myname\Favorites
C:\Users\Myname\Links
C:\Users\Myname\Saved Games
C:\Users\Myname\Searches
:: Exclude files matching these names
/XF
C:\Users\Myname\ntuser.dat
*.~bpl
:: Exclude files with any of the given Attributes set
:: S=System, H=Hidden
/XA:SH
:: Copy options
/S :: copy Subdirectories, but not empty ones.
/E :: copy subdirectories, including Empty ones.
/COPY:DAT :: what to COPY for files (default is /COPY:DAT).
/DCOPY:T :: COPY Directory Timestamps.
/PURGE :: delete dest files/dirs that no longer exist in source.
:: Retry Options
/R:0 :: number of Retries on failed copies: default 1 million.
/W:1 :: Wait time between retries: default is 30 seconds.
:: Logging Options (LOG+ append)
/NDL :: No Directory List - don't log directory names.
/NP :: No Progress - don't display percentage copied.
/TEE :: output to console window, as well as the log file.
/LOG+:c:\apps\commands\backup-robocopy-log.txt :: append to logfile
backup-robocopy-movemouse.bat
#echo off
#REM Move mouse to prevent maching from sleeping
#rem while running a backup script
echo Keep system awake while robocopy is running,
echo this script moves a mouse once in a while.
set basedir=%~dp0
set IDX=0
:LOOP
IF NOT EXIST "%basedir%backup-robocopy-lock.txt" GOTO :EOF
SET /A IDX=%IDX% + 1
IF "%IDX%"=="240" (
SET IDX=0
echo Move mouse to keep system awake
c:\apps\commands\nircmdc.exe sendmouse move 5 5
c:\apps\commands\nircmdc.exe sendmouse move -5 -5
)
c:\apps\commands\nircmdc.exe wait 1000
GOTO :LOOP
Looks like /e option is what you need, it'll skip same files/directories.
robocopy c:\data c:\backup /e
If you run the command twice, you'll see the second round is much faster since it skips a lot of things.

Xcopy files and folders into a new folder within the source folder

I have a little structure like this:
[ ] root
|
+---- text.txt
+-[ ] folder
+-[ ] copy
Now I'd like to copy all contents (including folder and its possible contents) of root into copy with Xcopy.
How would I do that?
If you really want to do as you ask, you could do it this way:
xcopy root root\copy /E
However, I would not advise that, because you would be making copies of copies since copy is a folder under root.
I would advise a different structure whereby copy is at the same level as root, and you could:
xcopy root root_copy /E
The documentation can be found at various places, depending on your operating system, such as:
http://support.microsoft.com/kb/289483
http://technet.microsoft.com/en-us/library/bb491035.aspx
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
Here's what I typically use:
xcopy src dest /Q /H /E /I /Y
/Q - Does not display file names while copying.
/H - Copies hidden and system files also.
/E - Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/I - If destination does not exist and copying more than one file, assumes that destination must be a directory.
/Y - Suppresses prompting to confirm you want to overwrite an existing destination file.

Having XCopy copy a file and not overwrite the previous one if it exists (without prompting)

I'm sending commands to a remote computer in order to have it copy a file.
I want the file to be copied, but not to overwrite the previous file with the same name (if it exists).
I also need the command to run without any prompts (xcopy likes to prompt whether the target name I've specified is file or directory, and it will also prompt about overwriting a file).
I have good results with xcopy /d.
It will copy NEWER files, and since we can assume that existing files have same time-stamp, you will copy only files that don't exist.
just in case anyone else finds this:
robocopy x:\sourcefolder Y:\destfolder /s /e /r:0 /z
much better than xcopy, even gives you a table at the end informing of any failed or skipped files. Doesn't prompt to not overwrite.
Well, there's a certain remedy! It has helped me with saving much of my effort and time on Win10 while writing a setup for our product demo.
Just try to use piping:
#ECHO N|COPY /-Y SourceFiles Destination
As an example I used this piece of code so that I would have a clean gentle quiet and safe copy!
#FOR /D %%F in ("FooPath") DO #(
#ECHO N|COPY /-Y ^"%%~npdxF\*.*^" ^"GooPath^" 3>NUL 2>NUL >NUL
)
where obviously FooPath is the source and GooPath is the destination.
Enjoy!
(main source: https://ss64.com/nt/copy.html)
Following command copy files and folder but not override file if already exist.
xcopy "*.*" "C:\test\" /s /y /d
No way to make it NOT overwrite as far as I know. but /Y will make it overwrite. and /I will get rid of the file/dict prompt. See xcopy /? for all options
You can also use the replace command. It has two modes: to add files that don't exist there or replace files that do exist. You want the previous mode:
replace <path1> <path2> /A
I had to copy AND rename files, so I got the prompt about creating a file or a directory.
This is the, rather "hackish" way I did it:
ECHO F | XCOPY /D "C:\install\dummy\dummy.pdf" "C:\Archive\fffc810e-f01a-47e8-a000-5903fc56f0ec.pdf"
XCOPY will use the "F" to indicate it should create the target as a file:
C:\install>ECHO F | XCOPY /D "C:\install\dummy\dummy.html" "C:\Archive\aa77cd6e-1d19-4eb4-b2a8-3f8fe60daf00.html"
Does C:\Archive\aa77cd6e-1d19-4eb4-b2a8-3f8fe60daf00.html specify a file name or directory name on the target
(F = file, D = directory)? F
C:\install\dummy\dummy.html
1 File(s) copied
I've also verified this command leaves existing files alone. (You should too :-)