Recursively move all files to one-upper directory - file

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.

Related

Create folder structure and copy file inside without specific path

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

Batch - Rename multiple files to sequential numbers

I need to rename multiple files at once. Lets say I have a these files:
episode1.mkv
e1.mkv
s01e01.mkv
As you see, the file names have nothing in common.
How can I change the names of all the files to numbers (1.mkv - 2.mkv - 3.mkv ...) using batch.
I want the first file to be renamed to 1.mkv (no letters or spaces or anything else in the name) the second file to be renamed to 2.mkv, and so on.
I've looked around the internet a lot and I still didn't find anything that does exactly this.
TIA
Batch file version
#echo off
setlocal ENABLEDELAYEDEXPANSION
set/a fileNum = 1
for %%f in (*.mp4) do (
ren %%~nf%%~xf !fileNum!%%~xf
set/a fileNum += 1
)
GUI version
FreeCommander can do this for you.
Run FreeComander
Navigate to the folder.
Select all the files you want to rename.
Hit F2
The rename dialogue will appear. To rename all files names to be numeric do this:
Click Rename to make the change.
I suppose you could make a FOR /L loop with a limit of the amount of files in your directory, feed it a DIR /B |FINDSTR .mkv line by line renaming each file to an index variable you keep. Since you are going to name them into just numbers your DIR command will spit the same movie back as the first item over and over so maybe write the output of DIR /B|FINDSTR .mkv to a text file and work off of that? Or move the file you rename to another folder?
Once you make a rough project we could help you refine it? Or use FreeCommander and make a batch to do the rest you want to do after that.

batch file which looks for specific files

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.

.bat coding or some app maybe?

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

Folder Names Based on Filenames

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.

Resources