how can I organize my directories automatically?, I have a "downloads" folder, which currently contains lots of different info, for example, work related info, tv-shows, movies, etc, software, etc.
How can I automatically, maybe using some .bat execution, not sure, check for example the name of the files, or the type, and put them in the right subfolders?.
Thanks!.
You can use the move command to move files. You can also use wildcards in it.
So you could write a batch script that looks something like this:
cd C:\Users\You\Downloads
rem Excel sheets are work.
move *.xls Work
rem Reports are work.
move Report*.pdf Work\Reports
rem Pron and other viewing material ;)
move *.mp4 Private
You could run this script automatically by making it a scheduled job. Note that this script changes to the right directory first and then moves items to a relative subdirectory. This means that the directories Work, Work\Reports and Private must exist in the Downloads directory.
Of course you can expand the script to make it check and create directories as well, or you can specify different paths if you want to move the files out of the Downloads directory.
Basically, try to do it on the command line and then try to generalize those steps into your script.
This batch file will create a set of folders like .mkv .jpg .mp3 using the filetypes inside the folder, and move the files into the appropriate folders.
Launch it from your desktop and change the "c:\media" to the right folder name.
#echo off
cd /d "c:\media" && for %%a in (*) do md "%%~xa" 2>nul & move "%%a" "%%~xa" >nul
Related
I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.
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
I need to create the same word documents basically every day to create reports. I would very much like to automate this using batch-script (or any script will do, really, as long as it also creates folders).
I have a .bat that creates the set of folders I need that looks like so:
#echo off
set /p dname= Enter Directory Name?
md %dname%
md %dname%
md %dname%\docx
md %dname%\xls
md %dname%\ppt
Now I need to copypaste a file to the docx folder that I have in the root folder, called intial.docx. This file needs to be renamed into
Initial_[Incrementing number]_V1_[Title].docx.
And I also need to set file properties of those files, namely Subject and Comments.
How do I do all that?
Well for the copy paste and renaming section, you could use the copy and rename commands:
#echo off
copy "directory\path\filename.docx" "directory\newpath\filename.docx"
rename "directory\newpath\filename.docx">"directory\newpath\newfilename.docx"
pause
That should work, let me know if you have any problems :)
For the properties, I guess you could just enter those into a text document,that's what I do.
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.