Create Zip of all files in a subfolder with a batch script - batch-file

I'm trying to create a batch script which is creating a .zip file including all files of a subfolder and which is named with the name of each subfolder.
If already tried to work with /f %%F but nothing seems to work. My currently working code looks like this:
for /d %%X in (*.*) do "C:\7zip\7za" a "%%X.zip" "%%X\"
which is giving me the correct named .zip file but without taking the files out of the subfolder so that my files look like this:
example.zip
-- example
---- example1.txt
---- example2.txt
I am thankful for any help. It feels like theres not too much to make it work like i need it!

Related

How can I create a folder with the same name as the file names in that folder?

What I want to accomplish can be done with for %%i in (*) do md "%%~ni" however this only works if my batch file is in the same folder as the files I want to process. I want to run a batch file from a another folder.
This is what I have tried so far and it's not working. It is still creating the folders in the same folder I run the batch file.
for %%i in ("D:\test1\*") do md "D:\test2\" "%%~ni"
What am I doing wrong? I have not written a script before.
You need to concatenate the path with the name
like this
for %%i in ("D:\test1\*") do md "D:\test2\%%~ni"

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.

I want to copy files, listed in a .txt file, into a new folder

I have roughly 2000 documents in one folder and I want to seperate them into different folders.
I have created a "documents.txt", which lists every file I´m interested in.
The .txt file reads as:
C:\Users\NIE\Desktop\test2\one.pdf
C:\Users\NIE\Desktop\test2\two.pdf
C:\Users\NIE\Desktop\test2\three.pdf
So now I went on creating a .bat file:
#echo off
FOR /F "delims=" %a IN (C:\Users\...\Desktop\test2\documents.txt) DO COPY "%%~a" "C:\Users\...\Desktop\test2\kopieren\%%~nxa"
The folder structure is like:
In "...\Desktop\test2", all documents are located. In a subfolder ("...\Desktop\test2\copy"), the specific documents (as listed in the documents.txt) should be copied.
While running my code, I´m getting the statement:
%C:\Users\...\Desktop\test2\one.pdf
The syntax for the filename, path is wrong
0 files were copied.
So I guess the "%" seems to be the bad guy here. I tried different styles for the .txt file, like
one.pdf
userprofile%\Desktop\test2\one.pdf (thought I could use the first % for completing the "%userprofile%" stuff
Every solution I could find via google did not worked either, the formatting of the .txt file seems to be a problem in my case.
Really looking forward for you answers :)
Looks like you have done a fatal mistake: %a
Fixed:
FOR /F "delims=" %%a IN (C:\Users\...\Desktop\test2\documents.txt) DO COPY "%%~a" "C:\Users\...\Desktop\test2\kopieren\%%~nxa"

Recursively move all files to one-upper directory

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.

Need help with batch-file

I have a very poor experience in writing batch-files.
And I need to do the following:
directory contents 2 files - .exe and .zip
I need to write batch file, merging these two files with resulting file named after zip-file.
In hardcoded variant it looks like this:
copy /b init.exe+archive.zip archive.exe
But it would be great if I could put in my directory zip-file with arbitrary name, click on my bat-file and get the exe-file with the name same as my archive's name.
P.S. (init.exe is never changes and directory will always containt only one zip-file at a time)
Thanks a lot for any help with this.
Something like this should do the job, I think:
FOR %%f IN (*.zip) DO COPY /B init.exe + "%%f" "%%~nf.exe"

Resources