creating a folder name with a batch file - datetime

I am trying to create a folder using batch file. The folder name should be in a format - yyyymmdd-hhmm .I got started with the below code but I get yyyymmdd- as one folder and hhmm as another folder. But when I tried it after 13.00 hrs I get yyyymmdd-hhmm format. Why is there a different behaviour during 9:45 in the morning. I don't know. Any help appreciated.
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
mkdir %mydate%-%mytime%
I get 1 folder -> 20160810- and another folder -> 945.

"I get 1 folder -> 20160810- and another folder -> 945."
That's because of the space, so mkdir sees two parameters and so creates two folders.
Either put qoutes around the new foldername
mkdir "%mydate%-%mytime%"`
or (maybe better) replace the space with a zero:
mkdir %mydate%-%mytime: =0%
putting qoutes around anyway doesn't harm:
mkdir "%mydate%-%mytime: =0%"
(btw: there is a way to get a date-time-string independent of local settings)

Related

Batch file to rename recursive folder

I saw this answer in the forum.
To rename the folder
C:\test\1\old
C:\test\2\old
C:\test\3\old
to become
C:\test\1\new
C:\test\2\new
C:\test\3\new
with the code
#echo off
FOR /D %%D IN ("C:\test\*") DO CALL :RENAME %%D
:RENAME
SET CRITERIA=\old
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "new"
It is working.
For me I want to add more
C:\test\1\old
C:\test\2\temp\old
C:\test\3\old
C:\test\1\new
C:\test\2\new
C:\test\3\new
The above code is not working for my above example with one more subfolder. Any help please.

Probleme with batch - script

i have 1000 photos with extension .jpg and every name of photo is a code like this 12345.jpg.
And i have 1000 folders with names like this: text_text_text_code(000000)
the job is put every photo in every single folder, i can solve this, but not working very well.
I found the problem, but i don't know how to solve.
in name folder i have text_text_text_code(000000), i need the code to stop when it arrives at character (.
#echo off
chcp 1250
echo.
echo.
cd "C:\Users\folderexample\Desktop\123"
for /r "C:\Users\folderexample\Desktop\imagem" %%a in (*.jpg) do (
for /f %%b in ('dir /b *"%%~na"*') do copy "%%a" "%%~fb"
)
I would like the search to go to the character ( and go to next search up until finish photos.

How to list contents of another directory without being in it?

Here is what my directory looks like:
Test ----
|
|----One
|
|----Two
I am attempting to list the contents of Two while still being in One and I am not able to do so.
I have tried this command (as seen in a other post) : "ls Test/" and it says No such file or directory. I have also tried ls Test/Two/ and it still does not work.
If you are in One and you want to list the contents of Two, you should go up to the parent directory using ..:
ls ../Two
../ will place you in the Test directory, from there, you can go to Two with no problem. If you have more depth levels, just add more ../ to go up one directory each time, but mind which is your current directory when running the command.
what does this command provide you in terminal.
ls -al Test //a flag is used for hidden file

How do I copy files recursively through a directory tree while appending parent folder names to the new files? Batch or VBScript

What I'm trying to do is copy a couple hundred images scattered throughout a directory tree and copy them all to another, single folder. I've found out how to do that with 'for /r %i in (*.jpg) ', but my main issue is that the files are all named using the same conventions. Sample:
Parent_Folder
001
file1.jpg
file2.jpg
002
file1.jpg
file2.jpg
The list continues as such. What I'm aiming to do is create this:
Dest_Folder
001_file1.jpg
001_file2.jpg
002_file1.jpg
002_file2.jpg
where the file's parent directory's name is added to the front of the files (so as to keep them listed in the same order). Not even individually going through every folder and using the "copy & rename" option will work since that will list all files named "file1.jpg" one after the other, "file2.jpg", and so on.
If there's any way to do this in a batch file, that would be much preferred, but a VBScript wouldn't be a bad choice either.
Thanks for any help!
EDIT
Magoo has given the perfect answer for my issue, but it seems his script only works if the source & destination paths have no spaces in them, despite the double quotes. I tried using this same script on another image collection, but with spaces in the paths, and I'm given an error saying the '[word after the first space in the path] was not expected at this time'. It was simple enough to just rename the folders, perform the copy, the rename them back, but is there a workaround for this?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /r "%sourcedir%" %%a IN (*.jpg) DO (
SET "newname=%%a"
SET "newname=!newname:*%sourcedir%\=!"
ECHO COPY /b "%%a" "%destdir%\!newname:\=_!"
)
GOTO :EOF
Grab each filename, remove the source directory name and a \, then copy the file to that name, changing each \ to _.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files.
Edit : fixed to allow spaces in sourcedir (simply quote after the /r in the for)
Sub BuildFileName(ParentFolder)
Dim objFSO, objFolder, strFileName
Set objFSO = WScript.CreateObject("Scripting.Filesystemobject")
Set objFolder = objFSO.GetFolder(ParentFolder)
For Each folder In objFolder.SubFolders
For Each file In folder.Files
strFileName = ParentFolder & "_" & folder.Name & "_" & file.Name
WScript.Echo strFileName
Next
Next
End Sub
Call BuildFileName("c:\scripts")

Powershell Script to rename TXT file based on certain criteria

I realise it is likely the each of the requirements listed below is available individually within the forum here but I am struggling to bring it all together (if at all possible!).
Hoping someone has the patience and time to point me in right direction to make this happen.
What I need to do is the following:
Scan a directory (and all sub-directories) for a particular filename
NOTE: Whilst there are many files within the sub-directories with the filename in question, we only wish to target those in a sub-directory with a suffix of JERRY
ie. In the below example the files indicated by the arrow would be targeted
ONE\NEW1-JERRY\FILENAME.TXT <----
ONE\NEW1-TOM\FILENAME.TXT
ONE\NEW1-SYLVESTER\FILENAME.TXT
TWO\NEW2-JERRY\FILENAME.TXT <----
TWO\NEW2-TOM\FILENAME.TXT
TWO\NEW2-SYLVESTER\FILENAME.TXT
THREE\NEW3-JERRY\FILENAME.TXT <----
THREE\NEW3-TOM\FILENAME.TXT
THREE\NEW3-SYLVESTER\FILENAME.TXT
FOUR\NEW4-JERRY\FILENAME.TXT <----
FOUR\NEW4-TOM\FILENAME.TXT
FOUR\NEW4-SYLVESTER\FILENAME.TXT
When file is found matching the filename and is within the sub-directory listed above take a copy of the file (to remain in same directory) & rename based on the following criteria:
a) Created date/time
b) Certain content within the file
The content in the file is always located on ROW 8 and it is the first 9 characters
Original filename: FILENAME.txt
Finished Product: FILENAME-20121129#1300-123456789.txt
Thanks in advance!
you should tell us what have you tried so far and what are your mains problems...
try this :
(remove the -whatif flag to actually copy files)
#list dir & subdirs
ls c:\ -recurse |
Foreach {
#find subdirs named JERRY
if($_.PSIsContainer -and $_.name -match "JERRY"){
ls $_.fullname -filter 'FILENAME*' |
Foreach{
$fcontent=get-Content $_.FullName
$content=$fcontent[7].Substring(0,9)
$newName=Get-Date -UFormat "%Y%m%d"
$destination="$($_.Directory)\$newName#$content.txt"
write-verbose $destination
Copy-Item $_.FullName -Destination $destination -WhatIf
}
}
}

Resources