How to individually compress all files in a folder with a batch file with 7zip? - windows

I've seen questions that I think are similar (but which I cannot make work for me), but I am very new to batch files so I don't understand what needs to be different to make it work. I have a folder /source/ with some files, say:
red.txt
orange.txt
yellow.txt
green.txt
blue.txt
purple.txt
I need to make a batch file that will run 7zip on all of them automatically to create zipped version of them. I found some resources which I thought would help but after a few hours of frustration, I think I'm forced to confront the fact that I simply don't know enough about Batch files to understand why it isn't working?
My effort was:
:: Changes directory to this file's directory
cd %~dp0
:: Location of source data
set dirSource="C:\Source\"
:: Target location of zipped data
set dirTarget="C:\Target\"
cd %dirSource%
for %f in (%dirSource%*.txt) do
(
7z.exe a "%dirTarget%\%%~NF.zip" "%dirSource%\%%~NXF"
:: based on related searches on the internet
:: but I can't find an explanation of _why_ this is supposed to work, which makes it hard to know why it isn't
:: ie, what does "a" do? What is ~NF vs ~NXF? etc
)
Lastly - I have been utterly unable to find a good comprehensive guide to batch files because of all the weird character modifiers. Since I don't expect to pick it all up in this question, if anyone has a good guide where I can look up what some of these seemingly random character combinations mean I would be forever grateful.

Related

Need help understanding a move script written for CMD involving multiple subdirectories into one directory

I got a folder with a lot of images in it, and I'd like to move everything to the main folder so basically I would like to turn this:
L:\Pixiv\Tags\44324\Image1.jpg
L:\Pixiv\Tags\4564356\Image2.jpg
L:\Pixiv\Tags\325423\Image3.jpg
L:\Pixiv\Tags\16324\Image4.jpg
...into this
L:\Pixiv\Tags\Image1.jpg
L:\Pixiv\Tags\Image2.jpg
L:\Pixiv\Tags\Image3.jpg
L:\Pixiv\Tags\Image4.jpg
I did some looking, and I discovered a move function for CMD, and while most of it I can understand, some of it I can't, and frankly, I'm just that smart enough to know what happens when you start playing around with stuff you don't fully understand, so I don't do it. Point is your basic move function is basically something like "move c:\Whatever C:\Whocares," but as you can tell from below, this is a little more complicated.
for /r %d in (*) do move "%d" "d:\all snaps\"
/r I think goes through all the folders at the current target directory. So if I'm in L:\Pixiv\Artists and I type in this code for L:\Pixiv\Artists as a destination folder every file in Artists is going to be dumped into Tags. Is this correct?
%d I couldn't make sense out of, so I've no idea what it does. Also, for ease of use, I would like to put this in a batch script, because boy howdy, do I got a lot of folders that needs to be fixed. From what little I was able to read I should write the same as above, but change %d to %dd, correct? Also where at would I add a target directory, so I could just write a batch script, and be done with it? Also, apologies in advance for my lack of knowledge, usually I use Batchrun when it comes to automating stuff.

How to move a folder that starts with a specific string in Batch

Im not scripting alot so i think this is total beginner stuff so
i have following problem, i want to move (like cut-out and paste) a folder ,that always starts with the same string but never have the same ending, to another folder.
Im working on a Windows Server that runs a Batch. The Batch is copying different Backup-Files to a Directory with a timestamp. This works fine because all the other Files and diretorys that i need to move have static names.
But im running into a problem with a folder/directory that has a dynamic name. The Folder starts always with the GUID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx} of the Application, so the start fo the directory name is static. But at the end, the Application is adding a underline _ followed by random charaters so the directory looks like following f.E. :
{11111111-1111-1111-1111-11111111111}_ehgwuh238th
This is how my other files with the static names are handled so far:
if exist %path%\%fullstamp% move %path%\Test %path%\%fullstamp%
path contains the path to the directory that includes the files that need to be moved.
fullstamp contains a timestamp which were used to create a new directory at the beginning of the batch.
I tried something like:
move %path%\Test* %path%\%fullstamp%
move %path%\Test*. %path%\%fullstamp%
move %path%\Test*\ %path%\%fullstamp%
But none of these worked out
So like i said probably not a big deal but i still need to ask.
To summarize it:
the directory that always starts/contains with the same string need to be moved to another directoy.
Some directoy names f.E.
Test_31tß0t30
Test_3tggwqgrgwrg
Test_ksmrh82ra
Thank you in advance and sorry for my bad english.
The move command seems to not accept wildcards for source directories, so you will need a for /D loop to resolve the path first:
for /D %%I in ("%ROOT%\Test*") do move "%%~I" "%ROOT%\%fullstamp%"
I also quoted both source and destination paths in order to avoid trouble with whitespaces or other special characters.
The fact that you use a variable %path% makes me think you set it to the root path of your operation, but PATH is a reserved variable used by the system to find executables, so you should not (over-)write it. That is why I used another variable name ROOT in my code suggestion.

Windows cmd, file rename and rearange

I'm a biologist, and sometimes I have to work with quite a lot of files, which are arranged not in an optimal way by someone.
Can you give me a tip on the following task?
There are folders
D:\.......\folder1\subfolder(1....)\subsubfolder(1.....) \SOMEtext_exactword(1.....)
(the last one are files without extension)
Can I create in windows cmd something like
D:\.......\folderEXACTWORD1\(subfolder1_subsubfolder1_SOMEtext_exactword1,
subfolder2_subsubfolder2_SOMEtext_exactword1,
subfolder3_subsubfolder3_SOMEtext_exactword1)
(the last one are files without extension again)
Meaning to find all files, containing "string" in their name in few sub folders, copy them into the specific folder and rename them, so they contain their parent file location in their name now.
I guess there should be a way to do it in a linux, though I am not a linux user and not a good (not at all actually) coder myself. Though I can handle some work, so I would be happy if someone can give me a tip whats the easiest way to do what I want. Or what can I search - I tried to search, but maybe I am missing some right words to look for.
Your question is very unclear, but here is more or less something that does this, you have to still define a few more items to make sure your find the correct files in locations and move them accordingly.
#echo off
setlocal enabledelayedexpansion
set mydir="D:\somedir\somedir\someotherdir\
for %%a in (%mydir%) do set "name=%%~nxa"
echo %name%
for /r %%i in (*string*) do (set "file=%%i"
echo move "!file!" "C:\somedir\!name!")
)
)
endlocal
Well. Someone elsewhere has given me advice and it worked. In total commander, you can do it with multi-rename tool and flags [P] and [G] which will allow you to add parent and grandparent directory name.
Well. I guess there is nothing wrong with having this topic with answer from myself, but I will be ok if it is deleted :)
Though I will be still interested if it can be done from cmd, just out of curiosity.

Xcopy creates blank/empty csv files

I have a batch file that I use (and have used all day successfully) to copy some badly named files (all are 1.csv) in a horrendously long dir tree to a different location with a more appropriate name (and shorter route to the file). It suddenly quit working; specifically, it IS making the new directory and it IS making the file with the correct name; however, the 'new' file is basically void. The source file may be 26mb but the destination file created is 287k. I'm using
xcopy "Z:\jobs\!q-z\clientname\tars\2016_06_06\home\abcd\xyz\qwert\more\moarr\deeper\deepest\x\y\z\1.csv" "Z:\jobs\!q-z\clientname\daily\160606\biz.csv*"
As I said earlier, this was working just fine all day long and suddenly began creating all dest files exactly the same size, which is an empty shell of a csv file. I have a feeling this might have to do with some sort of "cache" issue, but if so, I don't know how to clear it. Or is there some error in my syntax that allowed it to work almost accidentally before today? I've tried using pretty much every switch for xcopy, with no better results.
Also, the only thing that ever changes in the batch file is the date.

How to browse for file (Win7/64bits)

I need to quickly write a simple GUI over a command line application. Two steps are required:
Specify an input file path,
Specify an output file path (non existing)
I found out a previous post, however trying to get the above (1) to work seems like an insane task.
Indeed BrowseForFolder seems to only work in some weird cases, you can use BIF_BROWSEINCLUDEFILES for only *.pdf and/or *.txt (trial and errors). I can get an error if I select a *.dll and/or a *.jpg (don't ask).
So instead, I gave up this approach and followed another one, in this case (objIE.Document.all.FileSelect), only the name of the selected file is returned the path seems to be always set to "c:/fakepath" for some reason. So again I am missing the full path to be able to pass that to the command line app.
Is there any sane way (<10 lines of codes) to get (1) and (2) working on Win7/64bits (VBS, HTA...)?
Don't know if people are still interested in the BrowseForFolder file selection issue, but here's what I've found.
I had the same issue selecting files with BrowseForFolder using &H4000 / BIF_BROWSEINCLUDEFILES. I could get a return with .docx but not .doc files and as you say .pdf files.
For me .txt wouldn't return anything, as didn't WMI Backup .rec files that I needed for a script I'm writing, resulting in this error information:-
Error: Unspecified error
Code: 80004005
Source: (null)
After looking at other solutions I came back to this one as my preferred choice, plus it was doing my head in that it didn't want to work. So at the bitter end it seems to be this easy.
To get my .rec files recognized I add this to the registry:-
[HKEY_CLASSES_ROOT\.rec]
#="WMI.Backup"
[HKEY_CLASSES_ROOT\WMI.Backup]
#="WMI Backup"
"BrowseInPlace"="1"
To get .txt files recognized I add this to the registry:-
[HKEY_CLASSES_ROOT\txtfile]
"BrowseInPlace"="1"
So "BrowseInPlace"="1" seems to be the nugget.
Seems so unbelievably easy that I'm sure this solution is out there somewhere but I never came across it so thought I'd put it online.
I would be interested to find that it works for others as I fear that this issue may of sent me mad, still can't believe it seems to work.
Hope this helps.
Here are 3 different ways to do what you want:
http://www.robvanderwoude.com/vbstech_ui_fileopen.php

Resources