I am writing a batch script that does a myriad of things. The one part I am stuck on is copying files/file structure from a location to my final image. The file structure looks something like this
Foo
|->Bar
| |->Snafu
| | |-><FILES>
| |-><FILES>
|->Bar2
| |->Snafu
| | |-><FILES>
| |-><FILES>
|->Bar3
| |->Snafu
| | |-><FILES>
| |-><FILES>
etc...
I want to copy the whole contents of the Folder Foo while maintaining the file structure. Here is the rub...this has to be able to run on a clean copy of Windows, so I cannot use any third party programs (which leaves out XCOPY, etc.).
I have tried using the "copy" command with various parameters, but the closest I get is getting the files with no folder structure. I am not always sure what is in the folders, so I can't even hard code it.
I would appreciate any help. Thanks!
You can use XCOPY, which is way better than COPY.
XCOPY is NOT a third party command, there's no need for software. It was added back in 1986 (MS-DOS 3.30, correct me if I'm wrong), so every Windows OS has it.
Command would be: xcopy /y /h /i /e foo bar
Which will:
Copy all directories structure, including empty directories (/e)
It won't prompt for confirmation
Hidden files will be also copied.
Related
I have a bunch of folders that I would like to rename all the files contained within minus any subdfolders.
For example lets say I have two parent folders:
ParentFolder1 - [PF1]
ParentFolder2 - [PF2]
Each parent folder has various amounts of subfolders:
SubParentFolder1_1
SubParentFolder1_2
SubParentFolder2_1
Inside the ParentFolder and each SubParentFolder there can be files such as .mp3, .txt. etc. or more subfolders.
How would I go about renaming all and any files in this manner:
example.mp3 -> example - [PF1]
example.txt -> example - [PF2]
example.docx -> example - [PF2]
Appreciate any input!
This is a way to list files (not folders) in a range of directories and then do something with them... Specifics of renaming are up to you.
for FOLD in Parent*;
do for FILE in $(ls -p $FOLD | grep -v "/");
do echo "$FOLD/$FILE" "$FOLD/${FILE%.*}";
done; done;
Explanation:
For each folder (FOLD) in directories matching the wildcard Parent*,
list the contents, adding / to the end of directory names.
Do inverse grep on that list, leaving just the file names.
Loop through each FILE and echo out the original folder+file, followed by the folder and file with the suffix removed by patten matching.
Once you test this, you can replace echo with mv to do the actual renaming... (I've put these on separate lines to make them more readable, but would usually run this as one long command.
I have a scenario here, i am working on files clean up activity.
I have folders and withing the folder i have excel and pdf files. I want to retain the last two modified files and delete all other files.
Please help me writing the script.
Regards
NKS
To do something with all but the two most recently modified files can be done with a sort and then skipping two (broken over lines for ease of reading):
dir *.doc |
sort LastWriteTimeUtc -desc |
select -skip 2 |
# Do something like remove-item
(Using UTC to avoid problems when entering/leaving DST.)
I have following folder structure.
media
|
-----------------------------------
1day 2day 3day
| | |
--------- --------- ---------
| | | | | | | | |
hin eng fra hin ger spa hin eng vie
I want to copy all the subdirectories (1day, 2day, 3day) to a new folder. But I only want to copy hin and eng folders (and files inside it) in the new place.
Not sure how could it be done via Windows CMD.
Robocopy, xCopy commands doesn't itself have an include feature, they all have exclude feature. Because, by default they include everything within the folder. That is the reason why the same question was asked several times in past couple of years here and there on the web without a concrete solution.
Following command helps to break above rule by using "FOR" command and it works like a charm.
FOR /D /R %G IN (hin.*) DO ROBOCOPY "%G" "DRIVE:Folder\%~pG%~nxG" /E /S /NP
From the source folder, it will copy any directory that has the name "hin" and creates the same folder structure on destinated drive/folder.
There is GUI software which might help non-coders.
RichCopy via Microsoft (free, does have both include and exclude feature in advanced option, however include feature didn't work, for windows only)
https://technet.microsoft.com/en-us/library/2009.04.utilityspotlight.aspx
Easy RoboCopy (free, does not have include feature but has exclude feature, for windows only)
http://www.tribblesoft.com/home-page/easy-robocopy/
Both RichCopy & Easy RoboCopy is GUI of Robocopy command.
FastCopy (free, does have both include and exclude feature, for windows only)
https://fastcopy.jp/en/
RapidCopy MAC version of FastCopy (paid, for mac only)
GSCOPY PRO / GSRICHCOPY360 (paid, have both include & exclude feature, for windows only)
TeraCopy (paid, have both include & exclude feature, for windows only)
I have TRIED & TESTED all except RapidCopy which is in MAC. Also tested trial version of GSCOPY & TeraCopy. However, FastCopy wins it all.
I hope this helps.
_/_
I need to get a list of file names from a directory using a windows batch program. I would like to take each FILE NAME and combine that with another command line statement.
Note i only need the file name not the contents of the file.
How would this be done?
If i have a 'Data' directory on the D drive with the below files (note there could be many files)
--------------
myFile1.abc
myfile2.abc
------------------
How could i dynamically create something like this using a windows batch program?
move C:\myFile1.abc C:\newdir
move C:\myFile2.abc C:\newdir
note - (i know there is a easier way move files but but i am trying to understand the logic so i can use it in a different command)
You can use a for loop:
for %%X in (D:\*) do (
echo move %%X C:\newdir
)
Try on the command line:
for %X in (D:\DataFiles\*) do echo move "%~fX" C:\newdir>>logfile.txt
It puts all file names from D:\DataFiles in logfile.txt (except hidden files).
I have a simple xcopy script that I'm running from the command line that reads a CSV file of directories and file names. I've used a very similar script with no problems before. Here is the script:
Z:\HOME\>for /f "delims=, tokens=1,2,3,4" %i in (Z:\HOME\MissingImages.csv) do
echo f | xcopy "Y:\%j\%k\%l" "C:\Horizon\%j\%k\%l" >> Z:\HOME\MissingImagesLog.txt
However, it is not copying any of the files over
Here is an entry from the log file:
Does C:\Horizon\K\00\6bef500f.IMG specify a file name
or directory name on the target
(F = file, D = directory)? f
0 File(s) copied
It's finding the images because if I change the root directory to something else the script will just populate the log file with 0 File(s) copied for all entries, so the files are there and can be seen...
Also, the Z:\ drive is on a network and not local, but again I have used a very similar script across a network without problems (it just takes longer).
I've tried different options like /i, /s, etc. but I can't seem to get it to copy any files over.
xcopy will also report 0 File(s) copied if you use forward slashes "/" in paths instead of backslashes "\", though ONLY if you've enclosed the path in quotes.
This fails with "0 File(s) copied"
xcopy "pathname1/file" pathname2\file
This fails with "Invalid number of parameters"
xcopy pathname1/file pathname2\file
This works just fine
xcopy pathname1\file pathname2\file
It asks because it doesn't know whether you want to copy to directory (to be created) or you provide the full target pathname.
This will ask:
xcopy pathname1\file.from pathname2\file.to
However, adding slash will tell that you copy to directory:
xcopy pathname1\file.from pathname2\to\
But I haven't found the way to tell explicitly that I want to copy and rename file, except
echo Y | xcopy pathname1\file.from pathname2\file.to
I played a bit with your case (with for, do and xcopy) and found out that even if it asks Does SOMEFILE specify a file name or directory name on the target (F = file, D = directory)? it is provided with f from echo and it's copied successfully. Thus, it's not a problem with file/directory specifying, but with copying through network itself.
Well, that's annoying; I found the issue. It looks like when I generated my CSV file, it put a space at the end of each line, so xcopy was looking for files that had a space after the extension.
The thing that was throwing me off was that it was finding the files, but couldn't copy them, making me think it was a network or xcopy issue.
I just ran a sed script to remove the eol spaces and the xcopy script is now working as expected.