Copying a folder name to a variable in batch - batch-file

I have a folder at this location:
E:\Scripts\TEMPLATE\REC_DRAWER\1212314231_001001000
I would like using a batch script to copy that folder name into a variable excluding the _001001000
the 0 and 1 is randomized but is always the same length of characters.
I want to have a variable named folder_name with the following string content:
1212314231
I cant really wrap my head around how I can approach this,

Related

batch file variable to store file paths

I have multiple files with .arxml present inside a sub folder. I want to store the file paths in a variable and later this variable is used by a java command.
Folder1 inside this subfolders are present so my .arxml file can be present in any of the subfolders.
In a batch file I want to store in a variable all these file.arxml path names separated by a comma and this variable is used by a Java command.
Java command is present in same batch file.
Example
#echo start
Variable = c:/abc/h/f1.arxml , c:/efg/f2.arxml ....etc
Java command -winputs = variable
#Echo we are back

How to look at the contents of a zipped folder in batch?

In this case, I have a zip folder with contents inside. How would I look into the zip folder and be able to check the file name of those files for a certain word without having to unzip the file to begin with?
So for example:
I have a zip folder with the files inside named
123456789COW.txt
123445614COW.txt
123456789ASK.txt
232436787CAT.txt
and I want to check if the file has COW inside the name.
Every solution I have found so far has assumed I know the contents already, but in my case that may not always be true and I need to check.

How to replace text from a source file into different files

So I have been using Notepad++ to do some little clean-up tasks and now I am left with the biggest task..
I have a file called Artists.txt which looks like
Butta Mohamed
Daler Mehndi
Daljit Mattu
Darshan Khela
Davinder Deep
Davinder Deol
etc...
I have another file called Keywords.txt (located in hundreds of other folders). The folders are named like below and they all contain a text file called Keywords.txt
butta-mohamed-lyrics
daler-mehndi-lyrics
daljit-mattu-lyrics
darshan-khela-lyrics
davinder-deep-lyrics
davinder-deol-lyrics
The Keywords.txt contains the text _1 (several instances within the Keywords.txt).
What I would like to do is get each line from Artists.txt and have the _1 replaced. The folders are in the same order as Artists.txt.
So read Artists.txt get first line Butta Mohamed get first folder butta-mohamed-lyrics edit Keywords.txt find _1 replace (all) with Butta Mohamed. Save changes. Rinse and repeat so read Artists.txt get next line Daler Mehndi get next folder daler-mehndi-lyrics edit Keywords.txt find _1 replace (all) with Daler Mehndi. Save Changes.
Wondering if something like this is possible? Otherwise it would take me a week to manually do this via copy/pasting or even the replace function in Notepad++
I've tried the Macro function in Notepad++ but CTRL-V rather then pasting whats in the clipboard the macro seems to replace the CTRL-V function with whatever text the macro was recorded with has.
So just adding some extra information...
I don't have Notepad++ installed as my favorite text editor is UltraEdit (shareware).
Although Stack Overflow is not a free code writing service and we expect that the questioner shows us some programming efforts already made to solve a task, it was very easy for me to write the little UltraEdit script for this task and therefore here is an UltraEdit script for this task.
C:\\Temp\\Test\\ at top of the script must be replaced by path of parent folder for the *lyrics folders. UltraEdit scripts are executed with the JavaScript core engine. Strings in UltraEdit scripts are therefore JavaScript strings where backslash is the escape character. So it is necessary to escape each backslash in parent folder path by one more backslash.
To run this script in UltraEdit, open Artists.txt as first file in UltraEdit.
As second file create a new ASCII file with Ctrl+N, copy and paste the lines below into this new file, edit the parent folder path/name in script code and save this script for example with name KeywordsReplace.js into any folder.
Now run the script by clicking in menu Scripting on command Run Active Script.
You can see after script finished in automatically showed output window how many replaces have been made in which Keywords.txt files.
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Parent folder containing all the *lyrics folders.
var sParentFolder = "C:\\Temp\\Test\\";
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Select everything in first file.
UltraEdit.document[0].selectAll();
// Is first file not an empty file?
if (UltraEdit.document[0].isSel())
{
// Determine line terminator type for first file.
var sLineTerm = "\r\n";
if (UltraEdit.document[0].lineTerminator == 1) sLineTerm = "\n"
else if (UltraEdit.document[0].lineTerminator == 2) sLineTerm = "\r"
// Get all lines of first file into an array of strings
var asArtists = UltraEdit.document[0].selection.split(sLineTerm);
// Remove last string if it is empty because file ended with
// a line termination.
if (!asArtists[asArtists.length-1].length) asArtists.pop();
// Define once the parameters for all the replace in files executed
// below in the loop with changing directory and replace strings.
UltraEdit.frInFiles.filesToSearch=0;
UltraEdit.frInFiles.searchSubs=false;
UltraEdit.frInFiles.ignoreHiddenSubs=false;
UltraEdit.frInFiles.openMatchingFiles=false;
UltraEdit.frInFiles.searchInFilesTypes="Keywords.txt";
UltraEdit.frInFiles.regExp=false;
UltraEdit.frInFiles.matchCase=true;
UltraEdit.frInFiles.matchWord=false;
UltraEdit.frInFiles.logChanges=true;
UltraEdit.frInFiles.useEncoding=false;
UltraEdit.frInFiles.preserveCase=false;
// Run for each artist a replace of all occurrences of _1
// in the artists lyrics folder by name of the artist.
for (nArtist = 0; nArtist < asArtists.length; nArtist++)
{
// Build folder name by converting artists name to
// lower case and replacing all spaces by hyphens.
var sFolder = asArtists[nArtist].toLowerCase().replace(/ /g,"-");
// Define directory for replace in files by appending
// additionally the string "-lyrics" to folder name.
UltraEdit.frInFiles.directoryStart = sParentFolder + sFolder + "-lyrics\\";
UltraEdit.frInFiles.replace("_1",asArtists[nArtist]);
}
// The output window contains the summary information
// about the replaces made and therefore open it.
UltraEdit.outputWindow.showWindow(true);
}
}
Script was tested with the provided data with each Keywords.txt containing exactly 3 times _1 in the 6 *lyrics folders. Result of output window was:
Running script: C:\Temp\KeywordsReplace.js
============================================================
C:\Temp\Test\butta-mohamed-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\daler-mehndi-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\daljit-mattu-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\darshan-khela-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\davinder-deep-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\davinder-deol-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
Script succeeded.
In case of downloading and installing UltraEdit is not acceptable for you, you have to wait for another answer providing a batch file solution or a Notepad++ macro solution, or you make the necessary code writing by yourself.

How to copy a freshly created array containing files to another folder on the same computer

I am new to Powershell but have had experience with various programming languages including dBase III, basic, Fortran (shows my age). I am a musician and launch MP3 files as an accompanyment when I perform live, somewhat like karaoke. I have about 45 different MP3s that I use. I like to change the sequence of the songs that I perform, from time to time and this requires renaming the MP3 file names with a preceeding sequence number. The file names take on the form: "01 songnameA.mp3", "02 songnameB.mp3", "03 songnameC.mp3", etc. I also repeat songs so there might be an "09 songnameA.mp3" in the folder that I play back from. To do this, I typically create a text file that lists the songs in the order that I want to perform them. Each line in the text file has the form "01 Songname X" but no MP3 extension. I then manually copy the mp3 files into a folder and then edit the names applying a sequence number according to the text file. This is time-consuming.
I have created a Powershell script (version 2) that creates an array of the text file content and an array of the unnumbered MP3 song files. The script creates a 3rd array containing numbered MP3 filenames according to the sequence in the text file. This array does work and I can easily display the list of items in it which have the form "01 SongnameX.mp3". However, I have been unable to copy this array of MP3 filenames with a preceeding sequence number into another folder. I don't know how many variations of the "Copy-Item" statements I have tried but nothing works. The name of the array that contains the filenames is $nsfarray (new song file array). The command I am presently using:
$nsfarray | copy-item -Destination C:\temp
Returns the following error message:
Copy-item : Cannot find path 'C:\Users\My HP\Documents\My Scripts\01 A Good Time.mp3' because it does not exist.
The path is the default path that I use to run Powershell but somehow the MP3 file names get appended to it. The "01 A Good Time.mp3" is the first item in the $nsfarray. I know I am missing something here. Any suggestions would be appreciated.
This is really crude but the issue I see with your input file is that it obviously does not exactly match the real file name that you are looking to copy. So with that in mind lets try this on for size.
$musicPath = "c:\music"
$destinationPath = "c:\songset"
$nsfarray = "01 Age of Aquarius"
$nsfarray | ForEach-Object{
If($_ -match '^\d+ *(?<BaseName>.*)'){
Copy-Item "$musicPath\$($Matches.BaseName).mp3" -Destination "$_.mp3"
}
}
Work with explicit paths: $musicPath and $destinationPath so that we do not have to rely on our current location in PowerShell. Then we navigate each value in the array, which I have populated with one example. Now we need to extract the real file name away from your set list number. Using regex we return the part of the string that is not the beginning numbers and spaces and take everything after that.
After we simply copy the file that exists in the $musicPath folder that has the name "Age of Aquarius.mp3", which should be the real file, and copy it to the $destinationPath as "01 Age of Aquarius.mp3", which is how you want the file for your gig.
Clarification
After answering this and looking back at the question I think there is a chance I didn't understand it right. Please update the question if this is not the right way to address this. The core of your problem, if nothing else, is that you are not specifying a proper file path for the mp3s. You need to use the proper source folder and append the mp3 to the string.

How to create a String variable and use batch

I want to create a variable in a batch file and I want to assign a path of a text file to to this variable. After that, I want to use this variable to copy that file. How can I do this?
Thank you,
The general idea is this:
SET filename=c:\path\to\file.txt
COPY "%filename%" c:\destination
Note the quotes around %filename%: they are necessary to make the command work if the path or file name contains spaces.

Resources