I'm new to the site and tried to search for an answer a couple of days and found similar situations as mine, but not entirely the same and apologise in advance if there is an answer already somewhere.
I've created a batch script to make a small folder structure to organise my photos, and so far so good, it's working as expected here the code
#echo off
MD 1.Capture
MD 1.Capture\Selected
MD 1.Capture\Discard
MD 2.Projects
MD 3.Masters
MD 4.Web
MD 5.Instagram
Now the part I'm struggling with is copying the same file in each folder and subfolder.
The actual file will always be the same and is in a specific path that never changes, but I'd like to have the code to copy that file in the folders without specifying the folder's path, and instead make the folders and put the file inside no matter where the structure is, the reason why is that I will place the script in different places all the time and having to correct the path all the time will make the code useless to me. Is that possible?
And thanks in advance.
It wasn't absolutely clear to me which directories you wanted the source file, in this case C:\Users\Giovani\Pictures\Portrait.jpg, to be copied to, So I'm offering two complete batch-file options, where the final directories will be placed along side the batch file itself.
The first will copy to each 'new' directory, including 1.Capture:
#Echo Off
For %%G In (
"1.Capture"
"1.Capture\Selected"
"1.Capture\Discard"
"2.Projects"
"3.Masters"
"4.Web"
"5.Instagram"
) Do %SystemRoot%\System32\xcopy.exe "C:\Users\Giovani\Pictures\Portrait.jpg" "%~dp0%%~G\" /CHIKQRY 1>NUL
The second will do the same, creating the 1.Capture directory, but not copying to it:
#Echo Off
SetLocal EnableExtensions
For %%G In (
"1.Capture\Selected"
"1.Capture\Discard"
"2.Projects"
"3.Masters"
"4.Web"
"5.Instagram"
) Do %SystemRoot%\System32\xcopy.exe "C:\Users\Giovani\Pictures\Portrait.jpg" "%~dp0%%~G\" /CHIKQRY 1>NUL
Related
I'm wondering if a batch file could help to move all the files which i need from a couple of folders to a specific one. The folder structure looks like this example below:
d:\home\\(random)\upload\\*.*
d:\home\\(random)\uplaod\\*.*
The files in the directory upload should be moved to a specific folder. It can be done by putting every single path in to the batch file, (there are more then 100 folders and there will some more in the future). I guess there is an easier way to get this done.
Thanks for you help in advance.
for /d %%a in ("D:\home\*") do echo move "%%a\upload\*" "x:\destination\
This is the syntax for use in a batchfile. For use on commandline replace every %%a with %a.
The for /d returns subfolders of D:\home\. Just append the upload\* part.
I am absolutely brand new to any kind of development but need a batch job to copy a file from one folder to another.
The problem is that the source folder is dynamically named. The folder name will contain the current date and a suffix number (eg. "TestRun_20141106_13") - so I will never be able to determine the 'latest' version of the folder before running the batch / copy job.
Can anyone help please? I know this will be easy for someone but as I said, I am a complete noob!!
Thanks in advance.
Jamie
Yes, I havent been doing .bat for that long either, but i think i can help!
Here is a code for the movement of the file!
Dealing wiwth dynamically named folder...
#echo off
set /p txtfile=Filename without Path assumes c:\:
echo.%txtfile%
copy %txtfile% z:\testing\dealer.txt
echo Come back to this window when Agent is done with process. The copy file will be deleted.
#pause
copy %txtfile% c:\somefolder\namedsuccess\%txtfile%
del z:\testing\dealer.txt
exit
You will have to place your own variables in there my friend!
For moving of the files!
Easy part!
move /-y "Folder Path that files are in*(Any specific keyword?)*" "(Dest. folder)"
#ECHO OFF
FOR /F "TOKENS=*" %%A IN ('DIR "C:\Example" /s /b /a:d') DO SET CurrentDir=%%A
#ECHO.%CurrentDir%
Replace "C:\Example" with the Path your Folders are in,
save it to a File (.bat/.cmd) and execute.
The last step - Echo will return the most bottom foldername.
I'm wondering if it is possible to set up a batch command to perform this action.
Once .bat file is executed, ALL images from folders and sub-folders would be copied to my location on the desktop.
Example:
Original folder is located:
\intranet\file_location\PP Complete Images (in this folder will be loads of other folders and in those folders there will be .jpg images)
Destination file would be based on the desktop.
So I need to extract .jpg images from all folders and sub-folders.
If image already exists in original folder, skip the image or overwrite as script will be executed every morning.
Or should I look for a software to do this for me?
Existing code:
cd c:
cd\
copy "\\intranet\PP Complete Images\Master Image Folder*.jpg" "C:\Users\username\Desktop\Master Image Folder"
copy "\\intranet\PP Complete Images*.jpg"
exit
How do you want it?
It isn't quite clear to me, how the result exactly should be -- should it be flattened or should it be hierarchical as well?
Look at this for example:
source
folder-1
folder-1-1
image1.jpg
folder-1-2
image2.jpg
cheese.jpg
image3.jpg
some_text.txt
folder-2
folder-2-1
image3.jpg
some_music.mp3
cheese.jpg
target
Should the result be basically a copy of the shown hierarchy (without any other file than the jpgs), or should it be a flattened result like this one:
source
... (see above)
target
image1.jpg
image2.jpg
cheese.jpg
image3.jpg
image3.jpg
How can you do it?
Flattened
You can use DOS' for command to walk directories1 and make a custom function2 to handle the files:
#ECHO OFF
for /r %%f in (*.jpg) do call:copyFile %%f
GOTO END
:copyFile
copy /V /-Y %~1 ..\target
GOTO:EOF
:END
Meaning: for every %%f in the listing of *.jpg from the current working dir, execute function copyFile. The /r switch makes the listing recursing (walk through all subdirectories).
In the function, the argument passed to it (now known as %~1) is passed to the copy function: Copy the file to the target directory which is ..\target in this case. /V lets copy verify the result, /-Y lets it ask for permission to overwrite files. See copy /?!
Very big problem: If you have one or more files in different subfolders of your source directory, which have the same name (like the two cheese.jpgs in my example), you will loose data!
So, I wouldn't recommend this approach, as you risk loosing data (digital cameras are not very creative in naming pictures!).
Hierarchical
Just use robocopy:
robocopy /S <sourcedir> <targetdir> *.jpg
/S creates and copys subfolders as well. You can also use /DCOPY:T to make the directories have the same timestamp than the original ones or /L to preview the actions of robocopy.
Small problem: The /E switch handles subfolders as well, even if they are empty. /S handles subfolders as well, but not, if they are empty. But it handles them, if they are not empty, but have no JPG inside -- so, subfolders without JPGs will result in empty folders in the target folder.
Robocopy has loads of parameters, so check out robocopy /?.
Hope this helps! :-)
1Found here: How to traverse folder tree/subtrees in a windows batch file?
2Found here: http://www.dostips.com/DtTutoFunctions.php
Your existing code:
the cd c: is incorrect. To switch the current drive to c: use
c:
The cd \ is redundant. Your remaining code specifies the directories, so the current directory is irrelevant.
Your first copy command has three problems. Master Image Folder*.jpg means all filenames beginning Master Image Folder and ending .jpg. You probably meant Master Image Folder\*.jpg meaning all files ending .jpg in ...\Master Image Folder\
C:\Users\username\Deskto... is probably an error. It is a literal path, so the actual directory would be C:\Users\username\Deskto... You would probably need C:\Users\%username%\Deskto... to substitute-in the current username.
And then the job would stop on a filename-match, so either you'd be pressing A to overwrite all or you'd be pressing y or n for each name-match.
Your final copy command has no specified destination directory.
You can edit-in your actual code by using the edit button under the original text window, cutting-and-pasting your actual code - censoring if necessary, selecting the resultant code block and pressing the {} button above the edit box which indents each line with the effect of formatting and hilighting the code.
The simplest solution is probably to use
xcopy /d /y /s "\\intranet\PP Complete Images\Master Image Folder\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
which will copy updated files (/d) with automatic overwrite (/y) and scanning subdirectories (/s) from-name/mask to-directory.
This would create an identical directory-hierarchy to the original subtree under the destop's Master Image Folder directory.
You could extend this to
for %%a in (
"\\intranet\PP Complete Images\Master Image Folder"
"\\intranet\wherever\somewhere"
) do xcopy /d /y /s "%~a\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
to perform the same action on multiple directory-subtrees; but you need to ensure that the destination directory is not within any subtree selected for inclusion in the list within the parentheses.
I'd advise against "flattening" the output because if you do that, the latest whatever.jpg from each of the subtrees will end up in your destination directory, without notification that there are many possibly different whatever.jpg versions.
I do believe the solution to your problem would be Robocopy.
Robocopy is just plain awesome!
Here is the syntax of robocopy-
robocopy [Source] [Destination] [File] [...] [options]
Source
Specifies the source folder. Where you want to take the files from.
Destination
Destination directory/folder.
File
Here we are! This is what will help you. Here you can specify an extension you want to move. So in your case, your code would look somewhat like this.
robocopy *.jpg c:\destinationdir /S /MAX:1048576
*To execute this .bat every morning go to a program called task scheduler, dont worry, its built into windows. http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7
*Then Click on Create basic task, and set your task to whenever you like!
Thanks guys for your help!!!
I got it solved and there is a code below if someone would ever need something similar:
pushd Z:\intranet\PP Complete Images\
for /r %%a in (*.jpg) do (
XCOPY /Y "%%a" "C:\Users\username\Desktop\Master Image Folder"
)
popd
So here are the questions:
I have a folder, let's say C:\myFolder, and in this directory, I have many subfolders, and in each of this subfolders, I have exactly one folder, that contains pdf files, so my file structure looks something like this: C:\myFolder\someFolderInMyFolder\theOnlyFolderInThisFolder\*.pdf, now I want to move all these pdfs one level up, such that it will be like this: C:\myFolder\someFolderInMyFolder\*.pdf. Are there any command line commands, or scripts (that can be executed by Cygwin) that will help me with this?
What could complicate the situation is that, I have manually move some files one level up by myself, so it will help if there is a check condition.
I have some .zip files that the name are generated by computers, in the format of mm/dd/yy/fileIndex.zip, and the fileIndex is like No.001, for example. When I upload the extracted folders to Dropbox, and view the files on my iPad, it looks weird because the full folder name can not be displayed completely, so I want to rename each folder to someIndex, in the above example, from No.001 to 001, so same question here: any command or shell scripts?
You can move all PDFs up one level with a slightly modified version of what #Endoro suggested:
#echo off
for /r "C:\myFolder" %%f in (*.pdf) do move "%%~ff" "%%~dpi.."
However, there is no generic way for the script to distinguish files you already moved from files that have yet to be moved. It might be best if you undid your manual moves. Otherwise you'll have to find some distinguishing feature or check each name against a list of names, e.g. like this.
You can rename files like this:
#echo off
setlocal EnableDelayedExpansion
for /r "C:\myFolder" %%f in (No.*.zip) do (
set name=%%~nxf
set name=!name:No.=!
ren "%%~ff" "!name!"
)
endlocal
FTR, I somehow doubt that you really have files with names like mm/dd/yy/fileIndex.zip. Forward slashes are not valid characters for file names in Windows.
This isnt really a coding question. I need to put all my files into individual directories so each file has its own directory with the name based on the filename.
So before i go make an application to do this, does anyone know any software that can do this? like Automator or something.
No need to build an application. This simple one liner run from the Windows command line will move each file in a directory into a sub-directory based on the root name of the file.
for %f in (*) do #(md "%~nf"&move "%f" "%~nf")>nul 2>&1
Two files with the same base name but different extensions will be moved into the same directory. For example, "test.txt" and "text.doc" will both be moved into a directory named "test"
Any file without an extension will not be moved.
If you want to run this from a batch file, then
#echo off
for %%f in (*) do (
md "%%~nf"
move "%%f" "%%~nf"
) >nul 2>&1
You requirements were not very clear. If your requirements differ, then the script can probably be modified fairly easily to suit your needs.