August 23, 2022 at 4:23 am
Hello, I need help to rename a folder name from 20220822, which is the current date to anything, "newdirname", for example.
"C:\Test\20220822\"
I would like to use a sql script.
Or, if anyone knows how to get this done using PowerShell, I will appreciate feedback.
Thanks
August 23, 2022 at 7:35 am
In your server you can enable xp_cmdShell
exec sp_configure 'xp_cmdShell'
To view the current setting for this option. You can run CMD commands within your script with this (be aware of security vulnerabilities however). Google this setting to learn about it.
Are you trying to do this within a t-sql script ? I ask because you also threw powershell in there.
----------------------------------------------------
August 23, 2022 at 3:57 pm
Hi, this is what I am trying to do,
DECLARE @todays AS VARCHAR(20)
SELECT @todays = format(cast(getdate() as date),'yyyyMMdd')
EXEC xp_cmdshell
'copy C:\Test\@todays\ *.* C:\Newtest\';
But, I can figure out the syntax to pass the @todays to replace the date.
Thanks
August 23, 2022 at 8:09 pm
Hi, this is what I am trying to do,
DECLARE @todays AS VARCHAR(20) SELECT @todays = format(cast(getdate() as date),'yyyyMMdd')
EXEC xp_cmdshell 'copy C:\Test\@todays\ *.* C:\Newtest\';
But, I can figure out the syntax to pass the @todays to replace the date.
Thanks
Even for something so small, I strongly recommend getting out of the habit of using FORMAT for anything. It's like practicing the piano... unless you're practicing a comedy act based on the piano, you don't want to practice hitting the wrong keys. Here's an article that explains the beginning of why that's my recommendation...
https://www.sqlservercentral.com/articles/how-to-format-dates-in-sql-server-hint-dont-use-format
--Jeff Moden
Change is inevitable... Change for the better is not.
August 24, 2022 at 12:33 am
Jeff, format date is not the issue.
If you are willing to waste your time, do it on the real problem, the variable is the issue.
DECLARE @todays AS VARCHAR(20) SELECT @todays = format(cast(getdate() as date),'yyyyMMdd')
EXEC xp_cmdshell 'copy C:\Test\@todays\ *.* C:\Newtest\';
But, I can figure out the syntax to pass the @todays to replace the date.
August 24, 2022 at 1:41 am
Something like the following ?
declare @cmdStr as varchar(100);
declare @today AS CHAR(8)
set @today = '20220805'; /* Or however you want to set this */
set @cmdStr = 'copy C:\Test\'+ @today+ '\ *.* C:\Newtest\';
Select @cmdStr;
EXEC xp_cmdshell @cmdStr;
----------------------------------------------------
August 24, 2022 at 1:47 am
I might as well add, if this is supposed to be routing you could create a JOB that runs powershell to do the move. With powershell you can actually go recursivley (meaning through subfolders) within C:\Test\ and move these contents to the final destination. The Job could be scheduled daily, weekly, however. The name of the folder within Test\ would not matter . Or you could declare a variable to only search within that folder.
Powershell is worth learning if you've never tried it. So many resources on line to get you started.
----------------------------------------------------
August 24, 2022 at 2:55 am
Jeff, format date is not the issue.
If you are willing to waste your time, do it on the real problem, the variable is the issue.
DECLARE @todays AS VARCHAR(20) SELECT @todays = format(cast(getdate() as date),'yyyyMMdd')
EXEC xp_cmdshell 'copy C:\Test\@todays\ *.* C:\Newtest\';
But, I can figure out the syntax to pass the @todays to replace the date.
Uh huh... what do You use for a million rows? If I were a betting man, I'd say you probably use format. 😉
It's not a waste of time doing it the right way all the time.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 24, 2022 at 10:07 pm
Martin, your solution worked.
Thanks,
August 25, 2022 at 2:02 am
Martin, your solution worked.
Thanks,
So I can learn a bit, which one of Martin's replies does that cover?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply