How would I copy all files and folders except for hidden ones? - windows

On my drive S:\ I have a folder X which has multiple files and subfilders, each of which in turn contains its own files and subfolders and so on. Each folder(at any level) contains a hidden folder named the same way, say HID, with several files in it.
I have the same structure in another drive D:\ - same folder X with the same structure, but with slighly different contents in the files.
Basically I need to copy-and-replace the contents of X from S:\ into D:\, but not touch the hidden folders hamed HID (basically, they are unique in two independent ways - by the fact that they are named HID and by the fact that they are hidden).
I'm lazy to do this manually and don't feel like writing a C++ application to do this either. Is there any easy way to do this using a small bat file or a direct shell command with smart arguments?

You may be interested in xcopy command. As it says, "By default, xcopy does not copy hidden or system files.". It also has an exclude option, which seems to be used for ignoring specially named files.

Try this: lose the echo and pause if it's echoing the right commands.
#echo off
cd /d "s:\folder x"
for /f "delims=" %%a in ('dir /a-d /b /s ^|find /v "\HID\" ') do (
echo copy /y "%%a" "d:%%~pnxa"
pause
)

Related

Bash robocopy from text file containing path to folders and files to copy with special characters

I had a bash xcopy script, but got a lot of issues. After a lot of searching and reading I guess robocopy was a better tool to do the copy.
(The script must run on windows 10 computers without installing anything else and without internet access)
I'm trying to make a bash script that copy (with robocopy) some local network folders and files to a local custom directory. The aim is to be able to access to the files off from the local network.
The path to folders and files are stored inside a txt file (each line = a path)
I want to keep the structure of folder I save locally.
For example the folder X:\Path\to some\local\network\folder\with\some & characters\ will result in C:\PathTolocalFolder\Path\to some\local\network\folder\with\some & characters\ (without the X:\ letter)
Based on many similar questions (but not all at the same time) I have done this :
#echo off
SETLOCAL EnableDelayedExpansion
cls
cd C:
chcp 28591 > nul
for /f "delims=*" %%a in ('type "X:\path with spaces & specials characters\List.txt"') do (
REM echo %%a
REM echo !%%a!
echo %%a to C:\PathTolocalFolder!%%a!
ROBOCOPY "%%~a" "C:\PathTolocalFolder!%%a!" /S /A+:RA /R:1 /W:5
)
It is partially a success, but :
As there are special characters everywhere in paths and files names, I got some issues. Specially with & characters. My double quotes doesn't solve the problem. How could I go better?
For some cases, I want to save some files but not the whole directory where they are. The full path to the file is inside the text file. But as robocopy needs to add a space between folder path and file filter I have do some manipulation. How can I detect and extract the file name when there is one to adapt the robocopy command?
I want to use an exclusion list like I was doing before with xcopy. But robocopy doesn't accept a file in input for exclusions. I tried this to extract the exclusion file:
for /f "usebackq tokens*" %%D in ("C:\path to exclusion file\exclusions.txt") do (
if NOT "!dirs!"=="" (
Set dirs=!dirs! "%%D"
else (
Set dirs ="%%D"
)
)
But doesn't really know what I am doing and how to combine with the first part.
Bonus questions I'm using the robocopy log file functionality (removed from below) is there a way to archive (by adding the date in the name for example) previous log file before creating the new one? Is it possible to remove the progress percents in the log file but to display it in the terminal instead? How to use the "/np" option for log file but not for terminal display?
It's hard to me to understand how the delayed variables are working in batch files and how the different methods to read a file or variable are working.
Any help is welcome :)
Sorry for my bad English skills
thank for having read

Finding and removing ._ files with batch file

I'm trying to find and remove ._ files using a batch file. The files were copied from a Mac onto a PC.
ECHO Again!
DIR /B /A-D /ON /S ._* 2>nul
DEL /S ._*
It will list them but won't delete them. What's up with that?
D:\projects\._my_colours.txt
D:\projects\._png_vs_jpeg.jsx
D:\projects\._rainbow_screen_sizes.jsx
D:\projects\._save_it_mac.jsx
D:\projects\._some_text.txt
Could Not Find D:\projects\*._
I can delete them by hand, however, I just want to know where I'm going wrong.
The files carried over from MAC should have the archive and hidden properties only, so to ensure that you only pick those up, I'd suggest that you only select those for deletion. The DEL command has an /A option which selects the files to delete based upon their attributes. As soon as you use the /A option it picks up all attributes as if you'd selected them. If you do not exclude any attributes using the - prefix it will delete them all, (except for Read-only, unless you've included the /F option too). For example DEL /F /A ._* will delete those with Read-only, System, Hidden, Archive, unIndexed and reparse points (L). In this case your wanting those with just H and A, so to exclude all others you should use their exclusion prefix. Additionally as ._* is a global wildcard, you'll probably want to use the /Q option to prevent a prompted request for confirmation.
Del /S /Q /A:HA-R-S-I-L ._*
As a note-worthy point, if you have MAC directories transferred over too, those may have file partners. For example the ready for archiving hidden directory .Trashes will be partnered with a ready for archiving hidden file named ._.Trashes along side it. These would be deleted using the wildcard above, so if you have those type of directories, and you're not removing those too, you may wish to use a different method in order to preserve their partner files.
You could do that from the command-line via a for-loop. (From a batch-file you need to escape the % characters with another, %%):
For /F "Delims=" %A In ('Dir /B /S /A:HA-D-R-S-I-L ._* 2^>NUL') Do #If Not Exist "%~dpxA\" Del /A "%A"
In this case, the DIR command selects all of the files using the same methodology as the DEL command used, and passes those files as metavariables to the Do portion. We then delete each passed file, as long as it isn't a partner to a directory. We can do that using the /A option alone, because DIR has preselected only those we want. To perform the check for a partner, we just use a simple IF NOT EXIST statement, remembering that when checking for the existence of a directory, we use a trailing backslash. The check is performed by expanding each metavariable, %A to its drive:, \path\ and .extension using %~dpxA. The expansion of D:\MyDirectory\SubDirectory\._.Trashes would return D:\MyDirectory\SubDirectory\.Trashes so the check performed will effectively be If Not Exist "D:\MyDirectory\SubDirectory\.Trashes\". If that directory does not exist then it is deleted using Del /A "D:\MyDirectory\SubDirectory\._.Trashes".
System files! D'oh!
DEL /AH /S ._*

xcopy syntax? (batch)

#echo off
xcopy /s/z/i Q:\U1210.exe C:\Users\jalozinski\Desktop\
START C:\Users\jalozinski\Desktop\U1210.exe
So this purpose of this code is to copy U1210.exe from the Q:\ drive to the desktop, then start the newly copied .exe. But for some reason it copies random folders and files from the Q:\ drive, and I don't know why. I have a feeling it has to do with the /s/z/i (i was trying these so it may be oen of them) or something to do with the filepath of the source. I feel like if I close the filepath it won't fix anything though.
This is batch, by the way. :I
Lets look at what you have for the XCOPY:
/S = Copies directories and subdirectories except empty ones.
/Z = Copies networked files in restartable mode.
/I = If destination does not exist and copying more than one file, assumes that destination must be a directory.
First I would try w/o the /S, because you don't need directories.
I would include /R (Overwrites read-only files.)
I would also include /Y (Suppresses prompting to confirm you want to overwrite an existing destination file.)
This is what I got working the way I think you want it:
#ECHO OFF
set source=Q:\U1210.exe
set dest=C:\Users\jalozinski\desktop\
xcopy %source% %dest% /Z /R /Y
start %dest%\U1210.exe
exit
#echo off
xcopy Q:\U1210.exe C:\Users\jalozinski\Desktop\
START C:\Users\jalozinski\Desktop\U1210.exe
that should be all you need
/s is copying all the subfolders
/i is assuming it is a directory (when in doubt)
/z is a preventitive measure if you have a very slow computer
so you should not need any of those commands

A Windows batch file to c:\*.pst and copy them to a network drive without files with a duplicate name overwriting each other

My end users have Outlook pst files scattered all over their c drive. I came up with this batch file to find them and copy them to the end user's M drive.
rem **************************************************
#echo off
if not exist m:\migration mkdir m:\migration
if not exist m:\migration\pst mkdir m:\migration\pst
c:
dir /b c:\*.pst /s > m:\migration\pathdata.txt
pause
for /f "tokens=1 delims=" %%a in (m:\migration\pathdata.txt) do (copy "%%a" m:\migration\pst)
pause
BUT this paltry solution can't handle if the files have the same name, which is to be expected (archive.pst)
I was hoping to use xcopy to copy them and create their directory structure on drive M, that way files with the same names wont overwrite each other. But I have failed.
Any ideas? Thank you for helping me with this
I used this blog for help please check this one. It will search files and then copy to network location
http://tshootissues.blogspot.com
XCopy /-U c:\*.pst m:\migration\pst
The /U option will only copy files that already exist. The /-U should copy only those that do not already exist.
or
echo n|copy /-y c:\*.pst m:\migration\pst
source: http://www.dostips.com/forum/viewtopic.php?t=537
There is something you should observe with dir /b c:\*.pst /s > m:\migration\pathdata.txt
If you open your pathdata.txt or do for /f "tokens=1 delims=" %a in (m:\migration\pathdata.txt) do #echo %i you will notice you have paths like this:
C:\Documents and Settings\USERNAME\AppData\Local\Application Data\Application Data\Application Data\Microsoft\Outlook\FILENAME.pst
C:\Users\USERNAME\AppData\Local\Application Data\Application Data\Application Data\Application Data\Microsoft\Outlook\FILENAME.pst
And so on, but note it is actually the same file. This happens because dir /b c:\*.pst /s will go through folder shortcuts therefore you will have a .txt file with lots of path names pointing to the same .pst and when you xcopy you will copy the same file multiple times.
You can check this post which solves the problem then you can do the robocopy answer or modify it to your need.
Since you have multiple users and if your M drive is a network drive you could use m:\migration\%username%\pst to have it copied by user. That way if a user's pst have the same name as another user's pst you wont overwrite it and also you would be identifying them.
It is a general caution that PST file may get vulnerable threat on network driver or server. This truth is also accepted by MVP and Microsoft. So, it can use only at local drive only to avoid any problematic situation.

Windows Batch File Looping Through Directories to Process Files?

I need to write/use a batch file that processes some imagery for me.
I have one folder full of nested folders, inside each of these nested folders is one more folder that contains a number of TIF images, the number of images vary in each folder. I also have a batch file, lets call it ProcessImages.bat for Windows that you can "drop" these TIF files on (or obviously specify them in a command line list when invoking the bat); upon which it creates a new folder with all my images process based on an EXE that I have.
The good thing is that because the bat file uses the path from the folders you "drop" onto it, I can select all the TIFs of one folder and drop it to do the processing... but as I continue to manually do this for the 300 or so folders of TIFs I have I find it bogs my system down so unbelievably and if I could only process these one at a time (without manually doing it) it would be wonderful.
All that said... could someone point me in the right direction (for a Windows bat file AMATEUR) in a way I can write a Windows bat script that I can call from inside a directory and have it traverse through ALL the directories contained inside that directory... and run my processing batch file on each set of images one at a time?
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
#echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for %%f in (*.tif) do echo %%f
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Aacini's solution works but you can do it in one line:
for /R %%f in (*.tif) do echo "%%f"
Jack's solution work best for me but I need to do it for network UNC path (cifs/smb share) so a slight modification is needed:
for /R "\\mysrv\imgshr\somedir" %%f in (*.tif) do echo "%%f"
The original tip for this method is here
Posting here as it seems to be the most popular question about this case.
Here is an old gem I have finally managed to find back on the internet: sweep.exe. It executes the provided command in current directory and all subdirectories, simple as that.
Let's assume you have some program that process all files in a directory (but the use cases are really much broader than this):
:: For example, a file C:\Commands\processimages.cmd which contains:
FOR %%f IN (*.png) DO whatever
So, you want to run this program in current directory and all subdirectories:
:: Put sweep.exe in your PATH, you'll love it!
C:\ImagesDir> sweep C:\Commands\processimages.cmd
:: And if processimages.cmd is in your PATH too, the command becomes:
C:\ImagesDir> sweep processimages
Pros: You don't have to alter your original program to make it process subdirectories. You have the choice to process the subdirectories only if you want so. And this command is so straightforward and pleasant to use.
Con: Might fail with some commands (containing spaces, quotes, I don't know). See this thread for example.
I know this is not recursion (iteration through enumerated subdirectories?), but it may work better for some applications:
for /F "delims=" %%i in ('dir /ad /on /b /s') do (
pushd %%i
dir | find /i "Directory of"
popd
)
Replace the 3rd line with whatever command you may need.
dir /ad - list only directories
The cool thing is pushd does not need quotes if spaces in path.
rem Replace "baseline" with your directory name
for /R "baseline" %%a in (*) do (
echo %%a
)

Resources