I need to rename a file after its subdirectory in a batch file on Windows 8. For example, I need to rename "C:\path\to\my\logs\machine0015\001.log" to "C:\path\to\my\machine0015\machine0015.log"
I need to completely overwrite the file name with the last-dir name. There is only one relevant file per directory.
I can't work it out. It's been a long time since I've done any .bat programming; I've managed to do the rest of what I need, but that last point has me stumped. I've tried messing around with FINDSTR or a FOR loop, but I can't seem to work out how to extract the name of the last subdir from a full path (with random dir names and path depth).
I'm allowed to write a temp file, but an exe or 3rd-party app would be highly throwned upon, probably not permitted. This needs to work on Windows Vista/7/8, not XP.
Thanks for any help,
Olivier
try this:
for %%a in ("C:\path\to\my\logs\machine0015\001.log") do for %%b in ("%%~dpa.") do (
ren "%%~fa" "%%~nb%%~xa"
move "%%~dpa%%~nb%%~xa" "%%~dpb"
)
Related
Our programming archives contain tons of PLC programs (thousands of files)
Was recreating our backup structure, and wanted to filter through some of the junk. Made a batch file to delete all folders containing BAK with extension .acd, all files with .SEM, and .WRK, as these three are extra files that are created with opening the program, and are not needed. Some have gotten copied to the archives and duplicated many times.
I tested it on a copy of the folders, and wanted to run it routinely before the structure gets duplicated to other backup systems to prevent the backups from becoming cluttered again.
Here's the script I used:
del /q /s "Y:\Bays\*BAK*.acd"
del /q /s "Y:\Bays\*.Sem*"
del /q /s "Y:\Bays\*.Wrk*"
It deleted thousands of files, but as I watched I noticed three that did not make sense to me.
See the middle two deleted files:
"With Email" file:
These three were deleted, yet don't contain BAK in their names. I don't want to routinely run this if it will risk removing any copies of programs that aren't the automatically generated ones. Just hoping someone may be able to explain why these three were the only ones out of the thousands of deleted files to not follow the rule.
As you know, batch cannot delete files from directories with spaces between words. That's actually a bug that cannot be fixed, and wasn't built-in. So, I think that it's because of the unknown file extension, it happened the same to me. To make it known to your device, type regedit at the search bar, go to HKEY_CLASSES_ROOT, add a new key, and name it as the extension. Close regedit and try again.
Volume has property to enable generate short file names (8.3).
This property affects the execution of commands (del, for).
I have volume D where 8dot3 name creation is disabled
There is a file with name 1.abcd in folder.
Command for %i in (*.abc) do echo %i not find any files
I have volume C where 8dot3 name creation is enabled
There is a file with the same name 1.abcd in folder.
Command for %i in (*.abc) do echo %i find this file
Maybe if you use long file names then you need to disable generating short file names. You can do it with fsutil.
I have many files (Excel/CSV) contained in varied layers of sub-folders under a single directory. Many of the files contain a single cell with a common text string, which all need to be updated to a new string. The files all have different names. How difficult would it be to create a (preferably batch) script to search the entire directory (inclusive of sub-directories) for a given text string, replace all instances with a new string, and leave everything else in its place.
I am new to scripting, I have been searching the site and haven't found a solution that has worked for me. I want to stress that I cannot download, install or run third-party software due to security measures, and so applications like FART are out. Is anybody able to provide and input for the creation of something like this, or link me to such a script that already exists? Thanks in advance!!
Robust text editing using pure batch is difficult and slow.
Unless your admin techs have disabled CSCRIPT, you can use my JREPL.BAT - a hybrid JScript/batch text processing utility.
There is no download or installation process required. JREPL.BAT is pure script that runs natively on any Windows machine from XP onward. Simply copy the script into a new file named JREPL.BAT on your local machine.
Once you have your own copy, then all you need is something like the following command, run from the command console:
for /r "c:\your\root\path" %F in (*.csv) do #jrepl "search string" "replace string" /L /F "%F" /O -
I used the /L switch to treat the search as a literal. You may want to drop the /L switch and do a regular expression search instead.
If you put the command within another script, then you will need to double the percents and use CALL JREPL.
#echo off
for /r "c:\your\root\path" %%F in (*.csv) do call jrepl "search string" "replace string" /L /F "%%F" /O -
Issue the following command from the console prompt to get full documentation:
jrepl /? | more
I configure my console window with a large buffer height so I can scroll back to see prior output, thus I don't need | more when I look at the help.
Good day,
I am a CNC program not a computer programer. I am using CAM software to make cutting programs for our CNC router. The router is a bit old and can only take files 200-300 kb big. We are doing carvings that require 1-2 megs text files. I am using a program called GSplit ( http://www.gdgsoft.com/gsplit/ ) to divvy up the text file. It generates 10-25+ files with a custom header that our machine can read. All the files are great and it works, but I have to manually add the closing lines/footer to each file. The files that are created and used are normal .txt files but with a specific extension, .ANC.
Is there any way to automate this process of opening each individual file, scrolling to the end and copy/pasting the same 1-2 lines of code? The files are NAME[number].ANC in a contained folder. Would it be possible to just direct to a folder and say "add this 'text' to every file in this folder"?
Thanks for your time.
What OS are you using? Using Unix you can do a simple script on command line. If you are in the directory with the specific files simply execute:
for file in *; do echo "APPEND THIS" >> $file; done
If you are running Windows you should be able to do the same using cygwin (probably you could also use the power shell, but I don't know anything about the that)
I found a program Notepad++ (apparently the last person to find it...). USed the find/replace files option. A regular expression(note sure exactly what these are but I'm sure you guys do) "\s+\z" as to what to look for. It finds the last space or whatever at the end of all the files and then adds the code I need. Easy, free, and I don't need to write any computer code. Thanks for the attempt to help me Dirkk! :)
Could any one clarify me that, when uninstalling I need to delete everything form the Installation folder except the License file. How can I do it with NSIS scripting?
Thanks
Regards,
RoboAlex.
Instead of opening the file, as in Anders' third point, I'd do it this way:
Rename $INSTDIR\license.txt $PLUGINSDIR\license.txt
RMDir /R $INSTDIR # Remembering, of course, that you should do this with care
CreateDirectory $INSTDIR
Rename $PLUGINSDIR\license.txt $INSTDIR\license.txt
Depending on when it gets to the file it can't delete, RMDir /R may leave most of it behind, as I believe it will stop when it can't delete something; this way will get rid of it all properly. This will also lose the directory stats, but that's probably not important.
I'd recommend one of Anders' first two solutions over this, though. They're more precise.
Off the top of my head, there are 3 ways to do this:
Use Delete on one file at the time on a list generated at compile time with !system etc
Use FindFirst/FindNext/FindClose at runtime and Delete everything except the license based on filename
A bit of a hack, but you should be able to open the license file for write/append, then Delete/RMDir will not be able to delete the file since it has a open handle.
I've written a vary basic shell script that moves a specified file into the dustbin directory. The script is as follows:
#!/bin/bash
#move items to dustbin directory
mv "$#" ~/dustbin/
echo "File moved to dustbin"
This works fine for me, any file I specify gets moved to the dustbin directory. However, what I would like to do is create a new script that will move the file in the dustbin directory back to its original directory. I know I could easily write a script that would move it back to a location specified by the user, but I would prefer to have one that would move it to its original directory.
Is this possible?
I'm using Mac OS X 10.6.4 and Terminal
You will have to store where the original file is coming from then. Maybe in a seperate file, a database, or in the files attributes (meta-data).
Create a logfile with 2 columns:
The complete filename in the dustbin
The complete original path and filename
You will need this logfile anyway - what will you do when a user deleted 2 files in different directories, but with the same name? /home/user/.wgetrc and /home/user/old/.wgetrc ?
What will you do when a user deletes a file, makes a new one with the same name, and then deletes that too? You'll need versions or timestamps or something.
You need to store the original location somewhere, either in a database or in an extended attribute of the file. A database is definitely the easiest way to do it, though an extended attribute would be more robust. Looking in ~/.Trash/ I see some, but not all files have extended attributes, so I'm not sure how Apple does it.
You need to somehow encode the source directory in the file. I think the easiest would be to change the filename in the dustbin directory. So that /home/user/music/song.mp3 becomes ~/dustbin/song.mp3|home_user_music
And when you copy it back your script needs to process the file name and construct the path beginning at |.
Another approach would be to let the filesystem be your database.
A file moved from /some/directory/somewhere/filename would be moved to ~/dustbin/some/directory/somewhere/filename and you'd do find ~/dustbin -name "$file" to find it based on its basename (from user input). Then you'd just trim "~/bustbin" from the output of find and you'd have the destination ready to use. If more than one file is returned by find, you can list the proposed files for user selection. You could use ~/dustbin/$deletiondate if you wanted to make it possible to roll back to earlier versions.
You could do a cron job that would periodically remove old files and the directories (if empty).