Windows cmd, file rename and rearange - windows

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.

Related

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

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.

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.

Batch Script wanted (windows). find substrings in text file, and make specific changes based on positions of characters

(This is a Windows environment by the way.)
I'm new to batch scripting and, I'll be honest, I'm struggling.
I've done a lot of googling and spent a lot of time trying to work this out but I'm not getting very far. I thought I'd throw this out to the community to see if anyone was up to the challenge. :)
First up, this is for my own personal use and curious interest to see if this is possible without writing a new .exe in a more... conventional language.
This is it : I'm using some software which is essentially portable. the only downside is that it writes to a .ini file very specific file paths which may not necessarily be correct if using another computer.
For example, if I run the software from a USB in Computer A, that computer might see the USB drive as D: and save a path in the ini as Path=D:\portables\soft1\saves\file1.xyz. But if I later use the same USB in Computer B which assigns a different letter to the drive (let's say E:) then the software will throw its toys out of the pram, because it's looking on the wrong drive for what should be now on E:. And if I run the software from a network drive or even cloud storage then the path's get even more complex.
I want to run a script that might, before running the software's .exe, look in the .ini for all instances of such filepaths and replace the full path with simply Path=.\saves\file1.xyz. The software is happy with this and can find what it needs to.
This is the ONLY thing that prevents the software being portable.
This is how I thought it would be done...
Search each line in the .ini, one at a time, to see if contains the
substring "Path="
If it finds it do the following...
a. Store the position of the character AFTER the "=" in "Path=" in a variable (let's say it's vPosA=6)
b. Search the same line for another substring "\saves\" and if it finds that then store the position of the character BEFORE the first "\" in another variable (let's say vPosB=18).
c. If vPosA and vPosB are both >0 then do the following
i. Replace all the characters on that line, from vPosA to vPosB, to "."
ii. Reset vPosA and vPosB both back to "0"
Go to the next line and repeat the process until the end of file is reached, then overwrite the .ini with the changes.
I'm looking forward to seeing what people come up with. Like I said, it's curiosity driving this. If I get no answer, or it's way way too complex I may just write the thing in VB or something and use a .bat to run it before running the software. The advantage of having it as a Batch Script is that it could shared with the software's community and the script is plain to see for all. With a compiled .exe people won't be able to see what's in it and would therefore, justifiably, distrust it.
This should do:
#echo off
setlocal enabledelayedexpansion
(for /f "tokens=*" %%a in (in.txt) do (
set "line=%%a"
if /i "!line:~0,5!"=="Path=" set "line=Path=!line:*\saves=.\saves!"
echo !line!
))>out.txt
type out.txt
Read every line, check if the first five characters are Path= (ignoring capitalization), if yes replace all from start to (including) "\saves" with ".\saves, append the string Path= in front of it. Print the line. Redirect the whole thing to another file.
You can then delete the original file and rename the new file to the old name.
NOTE: this ignores empty lines. It's possible to keep them with some more code.

Dos dir mask, want "*.xxx" and not "*.xxxzz"

In my directories, I have file names like *.xxx and also *.xxxzz
When I do dir /s/b "*.xxx" I get *.xxxzz files in my list. I do NOT get these results in a "Take Command" console, but I do in a cmd console.
How do I get cmd to give me only *.xxx files?
With the DIR command, when you specify a mask containing an extension of exactly three characters, you will get matches of files that contain extensions with three or more characters, so long as the first three characters match the extension you originally specified.
I have no idea why it works this way, but at least the behavior is consistent nearly everywhere in the Windows API where you can specify a file search pattern. I can only assume it has something to do with support for long file extensions (i.e., file names that don't comply with the old DOS 8.3 rule).
But, you can get around the behavior in two ways:
A mask that specifies a file extension with one, two, or more than three characters will return only files with extensions of exactly the specified length.
So, for example, dir /s/b "*.xx" will give you only files with the extension .xx, and dir /s/b "*.xxxzz" will give you only files with the extension .xxxzz.
You can use the question mark wildcard character, instead of the asterisk. Asterisks mean "replaced by zero or more characters", while question marks mean an exact substitution of the question mark with a single character.
I suspect you're running into a problem because of the way Windows (older versions, at least) generated a short 8.3 filename to improve compatibility with old programs. You can probably confirm this by doing dir /x *.xxx in the directory where your *.xxxzz files exist.
I'm not sure if there's a way around it from the limited Windows command line tools. There should probably have been a switch on the dir command to force consideration only of long filenames, but I don't see one.
You may be able to solve your problem by disabling short filenames on that volume, if you're sure you don't need them for any ancient software you're running.
I haven't tried that myself, so maybe the short names already generated will continue to exist after you follow those instructions. If so, you might be able to fix it by copying the tree you're working with to a new location.
The fact is that unless the system has been set up to not generate 8.3 names, every file or directory with a long filename will also have an 8.3 alias. At least one - with some of the warped constructs in use in later editions, there many be many aliases.
Academically, since it's a matter of opinion (and hence outside of SO's bailiwick) it could be argued that your alternative command processor is not producing the correct results since it apparently ignores the short filename. Which is "correct" is debatable - what suits in one application may not in another. The easiest and most logical way of course is to have an option - but the chances of the major player in the debate incorporating such a facility at this stage amount to well,Buckley's
Here's a routine that may suit. It's not bullet-proof as it will have problems with some "poison characters" (those with special meaning for the standard command-processor cmd.exe(A windows application that emulates and enhances many of the facilities available in DOS, and normally, though technically-incorrectly, called "DOS" by the politically-incorrect for the sake of brevity.))
#ECHO Off
SETLOCAL
SET "mask=%~1"
IF "%mask:~-4,1%"=="." ECHO(%mask:~-3%|FINDSTR /L "* ." >NUL&IF ERRORLEVEL 1 (
FOR /f %%a IN ('dir /s/b "%mask%"') DO IF /i "%%~xa"=="%%~sxa" ECHO(%%a
GOTO :EOF
)
dir /s/b "%mask%"
GOTO :EOF

Resources